Python/File System: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
No edit summary
No edit summary
Line 24: Line 24:
CODE_PATH = os.path.realpath(os.path.dirname(__file__)) + '/'
CODE_PATH = os.path.realpath(os.path.dirname(__file__)) + '/'
DATA_PATH = os.path.expanduser('~/osm-data/')
DATA_PATH = os.path.expanduser('~/osm-data/')
</source>
|-
| Make directories. ||
<source lang="python">
import os.path
os.path.mkdir('dirname')
if not os.path.isdir('dirname'):
    os.path.makedirs('dirname')
</source>
</source>
|-
|-

Revision as of 05:31, 23 May 2018

TODO CODE
Read/Write file mtime
import os.path

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

shutil.copy(src, dst)
shutil.move(src, 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

os.path.mkdir('dirname')
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)