欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Python】 os

程序员文章站 2024-03-24 12:37:28
...
>>> import os
>>> os.name
'nt'
>>> os.getcwd()
'D:\\Python37'
>>> os.listdir()
>>> os.listdir()
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python.pdb', 'python3.dll', 'python37.dll', 'python37.pdb', 'python37_d.dll', 'python37_d.pdb', 'python3_d.dll', 'pythonw.exe', 'pythonw.pdb', 'pythonw_d.exe', 'pythonw_d.pdb', 'python_d.exe', 'python_d.pdb', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']
>>> python
'D:\\Python37\\python.exe'
>>> os.path.isdir(python)
False
>>> os.path.isfile(python)
True
>>> os.path.isabs(python)
True
>>> os.path.exists(python)
True
>>> os.path.normcase(python)
'd:\\python37\\python.exe'
>>> os.path.normpath(python)
'D:\\Python37\\python.exe'
>>> os.path.split(python)
('D:\\Python37', 'python.exe')
>>> os.path.splitext(python)
('D:\\Python37\\python', '.exe')
>>> os.path.getsize(python)
99856
>>> os.path.getatime(python)
1555915827.4082031
>>> os.path.getmtime(python)
1553523836.0
>>> os.path.getctime(python)
1553523836.0

>>> os.path.splitext(python)
('D:\\Python37\\python', '.exe')
>>> os.rename("unknown", "known")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'unknown' -> 'known'
>>> os.remove("unknown")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'unknown'
>>>

列出当前目录下的所有子目录

>>> [x for x in os.listdir('.') if os.path.isdir(x)]
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'Scripts', 'tcl', 'Tools']

列出所有的 .py 文件

>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
[]

上一篇: CSS3动画(transition和animation)

下一篇: