lang/python/ PythonRecipes1FilesAndDirectories


=== Paths Get absolute path from relative path. If you run python interactively, and type {{c import os print(os.curdir) }} you can get the useless no-shit-sherlock answer of {{c '.' }} kind of like answering the question of 'where are we?' with the dumb-and-dumberer answer of 'here!'. So how do we get from '.' to e.g. '/home/mrflibble/Music'? {{c import os.path os.path.abspath(os.curdir) }}

=== Walking tree {{c for root, dirs, files in os.walk(".", topdown=False): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name)) }}

=== File Permissions Make file executable {{c import os,stat

st = os.stat('filename') os.chmod('filename', st.st_mode | stat.S_IEXEC) }}%TIME=1632685227