Python/File System: Difference between revisions
< Python
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 28: | Line 28: | ||
</source> | </source> | ||
|- | |- | ||
| Make directories. || | | Make/Delete directories. || | ||
<source lang="python"> | <source lang="python"> | ||
import os.path | import os.path | ||
os.path.mkdir('dirname') # Standard | |||
os.path.mkdir('dirname') | |||
# | |||
if not os.path.isdir('dirname'): | if not os.path.isdir('dirname'): | ||
os.path.makedirs('dirname') | os.path.makedirs('dirname') # Recursively | ||
os.rmdir('dirname') # Only works for empty directory. | |||
os.removedirs('dirname') # Recursively | |||
</source> | </source> | ||
|- | |- | ||
Revision as of 06:42, 23 May 2018
| 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/Delete directories. |
import os.path
os.path.mkdir('dirname') # Standard
if not os.path.isdir('dirname'):
os.path.makedirs('dirname') # Recursively
os.rmdir('dirname') # Only works for empty directory.
os.removedirs('dirname') # Recursively
|
| List directory |
import os.path
items = os.path.listdir()
for i in items:
print(i)
|