Skip to content

Instantly share code, notes, and snippets.

@hdf
Last active June 9, 2023 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdf/7159128 to your computer and use it in GitHub Desktop.
Save hdf/7159128 to your computer and use it in GitHub Desktop.
Usage:dirlist.py c:\ > c_list.txt
import os, sys
cdir = '.'
if len(sys.argv) > 1:
cdir = sys.argv[1]
for dirname, dirnames, filenames in os.walk(cdir):
dirname = os.path.realpath(dirname)
ending = ''
try:
for subdirname in dirnames:
ending = subdirname
print('DIR: ' + os.path.join(dirname, subdirname))
for filename in filenames:
ending = filename
print('FILE: ' + os.path.join(dirname, filename))
except:
try:
print(bytes('Problematic name: ', encoding='utf-8') + \
dirname.encode('utf-8', 'surrogateescape') + b'\\' + \
ending.encode('utf-8', 'surrogateescape'))
except:
print(bytes('Extra problematic name: ', encoding='utf-16') + \
dirname.encode('utf-16', 'replace') + b'\\' + \
ending.encode('utf-16', 'replace'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment