파일 관리

python 2016. 1. 27. 14:48

-. remove(path), unlink(path)

파일 삭제

>>> os.remove("test.txt")

>>> os.unlink("test.txt")

 

-. rename(a,b)

a b로 이름변경, a b가 다른 경로면 이동(이름변경하면서 이동도 가능) #디렉토리 생성은 불가

#파일, 디렉토리 모두 가능

>>> os.rename("test.txt","test2.txt") #같은 디렉토리 이므로 이름만 변경됨

>>> os.rename("test.txt","data/test2.txt") #data 디렉토리로 이동과 함께 파일이름 변경

 

-. renames(a,b)

a b로 이름변경, a b가 다른 경로면 이동(이름변경하면서 이동도 가능) #디렉토리 생성 가능

#파일, 디렉토리 모두 가능

>>> os.renames("test.txt","test2.txt") #같은 디렉토리 이므로 이름만 변경됨

>>> os.renames("test.txt","data/test2.txt") #data 디렉토리로 이동과 함께 파일이름 변경

 

-. stat(path)

경로에 해당하는 정보를 반환

 

>>> os.stat(".")

nt.stat_result(st_mode=16895, st_ino=1407374883559076, st_dev=0, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1353267245, st_mtime=1353267245, st_ctime=1352382813)

 

>>> os.stat("test.txt")

nt.stat_result(st_mode=33206, st_ino=2533274790427368, st_dev=0, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1353266985, st_mtime=1353266985, st_ctime=1353266985)

 

 

-. utime(path,times)

경로에 해당하는 파일에 액세시시간,수정시간을 times로 수정

#times None일경우 현재시간으로 수정됨

>>> os.utime("test.txt",None) #파일의 액세스시간과 수정시간을 현재시간으로 변경

>>> os.utime("test.txt",(1,1)) #파일의 액세스시간과 수정시간을 [19700101 09:00:01]로 변경

 

 

-. walk(top[,topdown=True[,onerror=None[,followlinks=False]]])

top으로 지정된 디렉토리를 순회하며 경로, 디렉토리명을 순차적으로 반환

- test

  +-intest.txt

  +-game

    +-ingame.txt

    +-music

      +-inmusic.txt

위와 같은 디렉토리 구조에서 해당명령어 사용시

>>> for path,dirs,files in os.walk('test'):

       print(path,dirs,files)

test ['game'] ['intest.txt']

test\game ['music'] ['ingame.txt']

test\game\music [] ['inmusic.txt']

 

 

-. umask(mask)

umask를 설정, 수행시 이전mask값 반환

umask가 수행되면 이후 오픈되는 파일이나 디렉토리에 (mode & ~umask)와 같이 적용됨

 

 

-. pipe()

※ from os import * 사용시 import 안됨, from os import fdopen 가능 혹은 os.fdopen으로 사용가능

 

파이프생성, 함수를 실행하면 읽기, 쓰기 전용 파이프의 파일 디스크립터가 반환됨

파이프는 주로 부모프로세스와 자식프로세스간의 통신을 목적으로 사용함

>>> os.pipe()

(3, 4)

 

-. fdopen(fd[,mode[,bufsize]])

파일 디스크립터를 이용해 파일 객체를 생성

 

-. popen(command[,mode[,bufsize]])

※ from os import * 사용시 import 안됨, from os import popen 가능 혹은 os.popen으로 사용가능

 

인자로 전달된 command를 수행하며 파이프를 염

#Python3에서는 Popen클래스의 사용을 권장하지만 그대로 사용할수도 있음

'python' 카테고리의 다른 글

tkninter tk  (0) 2016.01.28
Tkinter 기초  (0) 2016.01.28
특수문자 사용  (0) 2016.01.27
time module  (0) 2016.01.27
sys module  (0) 2016.01.27
블로그 이미지

유정쓰

,