파이썬 Python - OS.PATH 기초 정리

파이썬 Python - 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()
'E:\\view\\Git\\Python’
Current Working Directory 를 보여준다.



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



True
>>> os.path.isdir(r'c:\awfewef')
False
>>> os.path.isfile(r'c:\python36\python.exe')
True
>>> os.path.isfile(r'c:\python36\python')
False
>>> os.path.isfile(r'c:\python36\afwef')
False
폴더인지 파일인지.. type또한 체크하며
폴더나, 파일이 실재로 존재하는지 확인한다.
두가지 모두만족해야 True이다.
>>> os.path.exists(r'c:\python36')
True
>>> os.path.exists(r'c:\python36\python.exe')
True
>>> os.path.exists(r'c:\python36\asdf')
False
>>> os.path.exists(r'c:\python36\ss.exe')
False
os.path.exists 는 존재하는지 확인한다. (파일, 폴더..)


>>> os.path.getsize(r'c:\python36')
0
>>> os.path.getsize(r'c:\python36\python.exe')
97944
파일의 크기를 보여준다.





>>> 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:\python36')
>>> os.getcwd()
'c:\\python36'
>>>
>>> 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'
현재 폴더에 새로운 디렉토리나, 파일명등을 붙이는 방법
실제로 있을 필요는 없다.
마지막 예제를 보면.. ".." 이 있는데..
상위 폴더로 이동할수 있다.


출처: http://chess72.tistory.com/132 [거울속 항해일지]


출처에서 가져와서 캡쳐화면 몇개 추가하였습니다.


We can best help you to prevent war not by repeating your words and following your methods but by finding new words and creating new methods.