PYTHON 프로그래밍

본문 바로가기
사이트 내 전체검색


PYTHON 프로그래밍
PYTHON 프로그래밍

3. OS.PATH 기초 정리

페이지 정보

작성자 관리자 댓글 0건 조회 3,663회 작성일 19-10-08 13:44

본문

3. OS.PATH 기초 정리

>>> sys.modules.keys()

현재 세션, 또는 프로그램안에서 사용되는 모든 모듈을 보여준다.

 


>>> sys.getrefcount(object)

 

 

 

>>> sys.exc_info()

returns a tuple with the latest exception's type, value, and traceback object

 


>>> try:

...     raise IndexError

... except:

...     print(sys.exc_info())

(<class 'IndexError'>, IndexError(), <traceback object at 0x00BAE8C8>)

 첫번째, 두번째는 directly print 해도 괜찮고

세번째는 traceback module 을 사용해서 처리한다.

 

 

>>> import traceback, sys

>>> def grail(x):

...     raise TypeError('already got one')

>>> try:

...     grail('arthur')

... except:

...     exc_info = sys.exc_info()

...     print(exc_info[0])

...     print(exc_info[1])

...     traceback.print_tb(exc_info[2])

...

<class 'TypeError'>

already got one

  File "<stdin>", line 2, in <module>

  File "<stdin>", line 2, in grail

========================================================

 

>>> os.getpid()

3744

유니크한 프로세스 ID를 보여준다.

os.getpid function gives the calling process's process ID

(a unique system-defiend identifier for a running program, useful for process control and unique name creation)

 

 

 


>>> os.getcwd()

'C:\\Documents and Settings\\Administrator

Current Working Directory 를 보여준다.

 

 

 


>>> os.path.isdir(r'c:\python32')

True

>>> os.path.isdir(r'c:\awfewef')

False


>>> os.path.isfile(r'c:\python32\python.exe')

True

>>> os.path.isfile(r'c:\python32\python')

False

>>> os.path.isfile(r'c:\python32\afwef')

False

폴더인지 파일인지.. type또한 체크하며

폴더나, 파일이 실재로 존재하는지 확인한다.

두가지 모두만족해야 True이다.


>>> os.path.exists(r'c:\python32')

True

>>> os.path.exists(r'c:\python32\python.exe')

True

>>> os.path.exists(r'c:\python32\asdf')

False

>>> os.path.exists(r'c:\python32\ss.exe')

False

os.path.exists 는 존재하는지 확인한다. (파일, 폴더..)

 

 

 

>>> os.path.getsize(r'c:\python32')


0

>>> os.path.getsize(r'c:\python32\python.exe')

26624

파일의 크기를 보여준다.

 

 

 


>>> os.path.split(r'c:\temp\test\python\hello.exe')

('c:\\temp\\test\\python', 'hello.exe')

파일부분과 폴더부분을 서로 잘라준다.


>>> os.path.join(r'c:\temp', 'hello.exe')

'c:\\temp\\hello.exe'

서로 합침..


>>> name = r'c:\temp\python\data.txt'

>>>

>>> os.path.dirname(name), os.path.basename(name)

('c:\\temp\\python', 'data.txt')

폴더, 파일을 보여준다. 파일이 없거나.. 형식에 맞지 않으면 아무것도 안나온다.

 

>>> os.path.splitext(name)

('c:\\temp\\python\\data', '.txt')
확장자만 따로 떨어뜨린다.

 

 

 

>>> pathname = r'c:\PP4thEd\Examples\PP4E\pyDemos.pyw'

>>>

>>> os.path.split(pathname)

('c:\\PP4thEd\\Examples\\PP4E', 'pyDemos.pyw')

>>>

>>> pathname.split(os.sep)

['c:', 'PP4thEd', 'Examples', 'PP4E', 'pyDemos.pyw']

>>>

>>> os.sep.join(pathname.split(os.sep))

'c:\\PP4thEd\\Examples\\PP4E\\pyDemos.pyw'

>>>

>>> os.sep

'\\'

>>>

>>> os.path.join(*pathname.split(os.sep))

'c:PP4thEd\\Examples\\PP4E\\pyDemos.pyw'

마지막 결과물에 주목할 필요가 있다.

c:\ 이게 아니라 c: 이렇게 나왔다.

 

 

 

 

>>> mixed

'C;\\\\temp\\\\public/files/index.html'

>>> os.path.normpath(mixed)

'C;\\temp\\public\\files\\index.html'
리눅스와 윈도우 디렉토리 포맷이 섞이거나, 잘못 써졌을때..
고치는 방법.

 

 

 

 


>>> os.chdir(r'c:\python32')

>>> os.getcwd()

'c:\\python32'

>>>

>>> os.chdir(r'e:\\')

>>> os.getcwd()

'e:\\'
폴더 이동하는 방법 COOL!!
정확하게 말하면 current working directory를 이동하는 거임

 

 

 

 

>>> os.chdir(r'e:\@work')

>>> os.getcwd()

'e:\\@work'

>>>

>>> os.path.abspath('temp')

'e:\\@work\\temp'

>>> os.path.abspath('temp\hello.txt')

'e:\\@work\\temp\\hello.txt'
>>> os.path.abspath(r'..\temp\hello.txt')

'e:\\temp\\hello.txt'
현재 폴더에 새로운 디렉토리나, 파일명등을 붙이는 방법
실제로 있을 필요는 없다.

마지막 예제를 보면.. ".." 이 있는데..

상위 폴더로 이동할수 있다.

 

댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.