import os
print os.listdir('.') # 현재 디렉토리의 파일 목록 얻기
print
print os.listdir('../') # 현재 디렉토리의 부모 디렉토리의 파일 목록 얻기
['.DS_Store', '.git', '.gitignore', '.ipynb_checkpoints', 'example', 'files', 'images', 'python01.ipynb', 'python02.ipynb', 'python03.ipynb', 'python04.ipynb', 'python05.ipynb', 'python06.ipynb', 'python07.ipynb', 'python08.ipynb', 'python09.ipynb', 'python10.ipynb', 'python11.ipynb', 'python12.ipynb', 'python13.ipynb', 'python14.ipynb', 'python15.ipynb', 'python16.ipynb', 'python17.ipynb', 'python18.ipynb', 'python19.ipynb', 'README.md', 'removeme.txt', 'sample.txt', 't1.txt', 'teststring2.pyc'] ['.DS_Store', 'calendar', 'calendar-dev', 'ipython', 'jms', 'jms-scheduler', 'mySpringFrame', 'python-e-learning', 'python-e-learning-movie', 'reviewer-recommend']
import os
def filetype(fpath):
print fpath, ':',
if os.path.isfile(fpath):
print 'Regular file'
if os.path.isdir(fpath):
print 'Directory'
if os.path.islink(fpath):
print 'Symbolic link'
flist = os.listdir('.')
for fname in flist:
filetype(fname)
.DS_Store : Regular file .git : Directory .gitignore : Regular file .ipynb_checkpoints : Directory example : Directory files : Directory images : Directory module_test.py : Regular file module_test.pyc : Regular file music : Regular file mymath.py : Regular file mymath.pyc : Regular file pickle.txt : Regular file pickle2.txt : Regular file python01.ipynb : Regular file python02.ipynb : Regular file python03.ipynb : Regular file python04.ipynb : Regular file python05.ipynb : Regular file python06.ipynb : Regular file python07.ipynb : Regular file python08.ipynb : Regular file python09.ipynb : Regular file python10.ipynb : Regular file python11.ipynb : Regular file python12.ipynb : Regular file python13.ipynb : Regular file python14.ipynb : Regular file python15.ipynb : Regular file python16.ipynb : Regular file python17.ipynb : Regular file python18.ipynb : Regular file python19.ipynb : Regular file python20.ipynb : Regular file python21.ipynb : Regular file README.md : Regular file removeme.txt : Regular file sample.txt : Regular file sample_new.txt : Regular file t.txt : Regular file t1.txt : Regular file t2.txt : Regular file
import os
def fileaccess(fpath):
print fpath, ':',
if os.access(fpath, os.F_OK):
print 'Exists',
else:
return
if os.access(fpath, os.R_OK):
print 'R',
if os.access(fpath, os.W_OK):
print 'W',
if os.access(fpath, os.X_OK):
print 'X',
print
flist = os.listdir('.')
for fname in flist:
fileaccess(fname)
.DS_Store : Exists R W .git : Exists R W X .gitignore : Exists R W .ipynb_checkpoints : Exists R W X example : Exists R W X files : Exists R W X images : Exists R W X module_test.py : Exists R W module_test.pyc : Exists R W music : Exists R W mymath.py : Exists R W mymath.pyc : Exists R W pickle.txt : Exists R W pickle2.txt : Exists R W python01.ipynb : Exists R W python02.ipynb : Exists R W python03.ipynb : Exists R W python04.ipynb : Exists R W python05.ipynb : Exists R W python06.ipynb : Exists R W python07.ipynb : Exists R W python08.ipynb : Exists R W python09.ipynb : Exists R W python10.ipynb : Exists R W python11.ipynb : Exists R W python12.ipynb : Exists R W python13.ipynb : Exists R W python14.ipynb : Exists R W python15.ipynb : Exists R W python16.ipynb : Exists R W python17.ipynb : Exists R W python18.ipynb : Exists R W python19.ipynb : Exists R W python20.ipynb : Exists R W python21.ipynb : Exists R W README.md : Exists R W removeme.txt : Exists R W sample.txt : Exists R W X sample_new.txt : Exists R W t.txt : Exists R W t1.txt : Exists R W t2.txt : Exists R W
os.chmod('sample.txt', 0777) # 리눅스에서의 실행 예
import os
os.rename('t.txt', 't1.txt') # t.txt를 t1.txt로 바꾼다
print os.access('t.txt', os.F_OK)
print os.access('t1.txt', os.F_OK)
False True
os.rename('t1.txt', 'example/t1.txt') # 현재 작업 디렉토리의 t1.txt를 example에 t1.txt이름으로 옮긴다.
print os.access('example/t1.txt', os.F_OK)
True
import shutil
shutil.copyfile('sample.txt', 'sample_new.txt')
print os.access('sample_new.txt', os.F_OK)
True
import os
print os.path.abspath('o.txt')
/Users/yhhan/git/python-e-learning/o.txt
f = '/Users/yhhan/git/python-e-learning/sample.txt'
print os.path.exists(f)
print os.path.exists('sample.txt')
print os.path.exists('asdf.txt')
True True False
print os.curdir #현재 디렉토리
print os.pardir #부모 디렉토리
. ..
print os.sep
/
f = '/Users/yhhan/git/python-e-learning/t.txt'
print os.path.basename(f) # 파일명만 추출
print os.path.dirname(f) # 디렉토리 경로 추출
t.txt /Users/yhhan/git/python-e-learning
print os.path.split(f)
('/Users/yhhan/git/python-e-learning', 't.txt')
print os.path.splitdrive(f)
('', '/Users/yhhan/git/python-e-learning/t.txt')
print os.path.splitext(f)
('/Users/yhhan/git/python-e-learning/t', '.txt')
import os
print os.getcwd()
/Users/yhhan/git/python-e-learning
os.chdir('/Users/yhhan/Public/')
print os.getcwd()
/Users/yhhan/Public
import os
os.mkdir('temp') # 0755 기본 모드(rwxr-xr-x)로 만들어짐
os.mkdir('temp2', 0700) # 0700 모드(rwx------)로 만들어짐
os.makedirs('temp/level1/level2') #0755 기본 모드, 중간에 필요한 디렉토리도 모두생성
print os.access('/Users/yhhan/Public/temp', os.F_OK)
print os.access('/Users/yhhan/Public/temp2', os.F_OK)
print os.access('/Users/yhhan/Public/temp/level1/level2', os.F_OK)
--------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-13-acd03c396068> in <module>() 1 import os 2 ----> 3 os.mkdir('temp') # 0755 기본 모드(rwxr-xr-x)로 만들어짐 4 os.mkdir('temp2', 0700) # 0700 모드(rwx------)로 만들어짐 5 os.makedirs('temp/level1/level2') #0755 기본 모드, 중간에 필요한 디렉토리도 모두생성 OSError: [Errno 17] File exists: 'temp'
os.rmdir('temp2') #디렉토리에 내용이 없을 때 삭제가능
os.rmdir('temp') #디렉토리에 다른 파일이 있으면 삭제할 수 없음
--------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-43-cb2ef2e59e2c> in <module>() ----> 1 os.rmdir('temp') #디렉토리에 다른 파일이 있으면 삭제할 수 없음 OSError: [Errno 66] Directory not empty: 'temp'
os.removedirs('temp/level1/level2')
import shutil
shutil.rmtree('temp')
os.mkdir('temp')
os.mkdir('temp/temp2', 0700)
shutil.copytree('temp', 'myweb_backup')
import os
os.chdir('/Users/yhhan/git/python-e-learning')
print os.getcwd()
print
for path, subdirs, files in os.walk(os.getcwd()):
for fname in files:
if fname.endswith('.pyc'):
fullpath = os.path.join(path, fname)
print 'removing', fullpath
os.remove(fullpath)
/Users/yhhan/git/python-e-learning removing /Users/yhhan/git/python-e-learning/module_test.pyc removing /Users/yhhan/git/python-e-learning/mymath.pyc
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일