python--os和os.path模块

时间:2024-03-30 22:29:45

>>> import os

>>> #curdir     #获取当前脚本的绝对路径
>>> os.curdir
'.'
>>> import os.path

>>> #获取绝对路径
>>> os.path.abspath(os.curdir)

'C:\\Users\\GUOGUO'


>>> #chdir      #修改当前目录
>>> os.chdir("../")

>>> os.path.abspath(os.curdir)
'C:\\Users'
>>> #打印cpu线程
>>> os.cpu_count()

12
>>> #获取当前路径
>>> os.getcwd()

'C:\\Users'
>>> #获取进程编号
>>> os.getpid()

61340
>>> #查看多进程
>>> os.getppid()

62024
>>> #获取系统名称
>>> os.name

'nt'
>>> #文件分割符
>>> os.sep
'\\'
>>> os.listdir()
['All Users', 'Default', 'Default User', 'desktop.ini', 'GUOGUO', 'Public']
>>> #查看当前文件夹所有文件夹名称
>>> os.scandir()
<nt.ScandirIterator object at 0x0000018BAE3A6850>
>>> re = os.scandir()



>>> os.path
<module 'ntpath' (frozen)>
>>> from os import path


>>>
>>> path
<module 'ntpath' (frozen)>
>>> dir(path)
'abspath', 'altsep', 'basename', 'commonpath', 'commonprefix', 'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdevdrive', 'isdir', 'isfile', 'isjunction', 'islink', 'ismount', 'join', 'lexists', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath', 'relpath', 'samefile', 'sameopenfile', 'samestat', 'sep', 'split', 'splitdrive', 'splitext', 'splitroot', 'stat', 'supports_unicode_filenames', 'sys'
>>>
>>>
>>> path.abspath(".")
'C:\\Users'
>>> path.altsep
'/'
>>> #查找文件名称
>>> f = "C:\\Users"
>>> path.basename(f)
'Users'
>>>
>>> path.exists(f)
True
>>> #'getatime', 'getctime', 'getmtime',文件访问时间,文件创建时间,文件修改时间
>>> #isabs      判断是不是绝对路径      ismount判断是否挂载'
>>> 拼接路径 path.join()

>>>


>>> #面试题     给定一个路径(如: d://),使用python代码,实现遍历该路径下的所有文 件
>>>