Python/File System

From Fundamental Ramen
< Python
Revision as of 06:38, 23 May 2018 by Tacoball (talk | contribs)
Jump to navigation Jump to search
TODO CODE
Read/Write file mtime
import os.path

mtime = os.path.getmtime('filename')
os.utime('filename', (mtime, atime))
Copy/Move/Delete file
import os
import shutil

shutil.copy(src, dst)
shutil.move(src, dst)
os.remove(dst)
Path conversion
import os.path

CODE_PATH = os.path.realpath(os.path.dirname(__file__)) + '/'
DATA_PATH = os.path.expanduser('~/osm-data/')
Make directories.
import os.path

# Standard
os.path.mkdir('dirname')

# Recursively
if not os.path.isdir('dirname'):
    os.path.makedirs('dirname')
List directory
import os.path

items = os.path.listdir()
for i in items:
    print(i)