Python/File System
< Python
Jump to navigation
Jump to search
| TODO | CODE |
|---|---|
| Read/Write file mtime |
import os
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)
shutil.rmtree(dir)
os.rename(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/Delete directories. |
import os
import os.path
os.path.mkdir('dirname') # Standard
if not os.path.isdir('dirname'):
os.makedirs('dirname') # Recursively
os.rmdir('dirname') # Only works for empty directory.
os.removedirs('dirname') # Recursively
|
| Change/List directory |
import os
os.chdir('sucks')
for i in os.listdir():
print(i)
|
| Relative path from source path See: StackOverFlow 1 StackOverFlow 2 |
from os.path import abspath, dirname
abspath(abspath(dirname(__file__)) + '/../../data')
|