python中os模块的使用(直观理解)

说真接触python也挺长时间了,关于os模块,一直都是有很浅的认知,只知道与操作系统有关系。今天,一定要把它的用法琢磨透。

os模块提供了很多的方法处理文件和目录 使用前要导入这个模块。

注释:

绝对路径就是文件的真正存在的路径,是指从硬盘的根目录(盘符)开始,进行一级级目录指向文件。
相对路径就是以当前文件为基准进行一级级目录指向被引用的资源文件。

1.os.path:相信大多小伙伴用这个属性最多,主要是用于获取文件的属性

--->os.path.abspath(path)                 #返回绝对路径

>>> os.path.abspath('aim.txt')
'G:\\python_learn\\python_setup\\aim.txt'

--->os.path.join(path1[,path2[,....]])    #将目录和文件名合成一个路径(django中设置静态文件路径)

>>> os.path.join('G:','python_learn','python_setup','os.py')
'G:python_learn\\python_setup\\os.py'

--->os.path.dirname(path)                              #返回文件的目录

>>> os.path.dirname('G:\\python_learn\\python_setup\\aim.txt')
'G:\\python_learn\\python_setup'

--->os.path.basename(path)                      #返回文件名

>>> os.path.basename('G:\\python_learn\\python_setup\\aim.txt')
'aim.txt'

--->os.path.split(path)                                #将路径返回一个dirname和basename

>>> os.path.split('G:\\python_learn\\python_setup\\aim.txt')
('G:\\python_learn\\python_setup', 'aim.txt')

--->os.path.getatime(path)                        #返回文件最近访问的时间(浮点型秒数)

>>> os.path.getatime('G:\\python_learn\\python_setup')
1581469835.3049662

--->os.path.getctime(path)                      #返回文件path创建时间

>>> os.path.getctime('G:\\python_learn\\python_setup')
1564967526.5744872

--->os.path.getmtime(path)                     #返回最近文件修改的时间 

>>> os.path.getmtime('G:\\python_learn\\python_setup')
1570664720.7153866

--->os.path.isabs(path)                        #判断是否为绝对路径

>>> os.path.isabs('G:\\python_learn\\python_setup\\aim.txt')
True

--->os.path.isfile(path)                        #判断路径是否为文件

>>> os.path.isfile('G:\\python_learn\\python_setup')
False

--->os.path.isdir(path)                       #判断文件是否为目录

>>> os.path.isdir('G:\\python_learn\\python_setup')
True
>>> os.path.isdir('G:\\python_learn\\python_setup\\aim.txt')
False

2.os.getcwd():查看当前工作目录

>>> os.getcwd()
'G:\\python_learn\\python_setup'

3.os.chair(path):用于改变当前工作目录到指定的路径

path:要切换的新路径

4。os.listdir(path):返回path指定的文件夹包含的文件或文件夹的名字的列表

>>> os.listdir('G:\\python_learn\\python_setup')
['chromedriver.exe', 'curl.exe', 'DLLs', 'Doc', 'etc', 'geckodriver.exe', 'include', 'Lib', 'Library', 'libs', 'LICENSE.txt', 'NEWS.txt', 'phantomjs-2.1.1-windows', 'phantomjs.exe', 'python-3.7.0-amd64.exe', 'python-3.7.0.exe', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'setup.cfg', 'share', 'tcl', 'Tools', 'twisted', 'vcruntime140.dll', '安装步骤.jpg', '新建文件夹']

5.os.mkdir(path):逐级创建目录,前提是前面的目录已存在          os.makedirs() :用于递归创建目录

os.mkdir(path)格式:不能越级创建

>>> os.mkdir('hi')
>>> os.getcwd()
'G:\\python_learn\\python_setup'
>>> os.listdir()
['chromedriver.exe', 'curl.exe', 'DLLs', 'Doc', 'etc', 'geckodriver.exe', 'hi', 'include', 'Lib', 'Library', 'libs', 'LICENSE.txt', 'NEWS.txt', 'phantomjs-2.1.1-windows', 'phantomjs.exe', 'python-3.7.0-amd64.exe', 'python-3.7.0.exe', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'setup.cfg', 'share', 'tcl', 'Tools', 'twisted', 'vcruntime140.dll', '安装步骤.jpg', '新建文件夹']

os.makedirs(path,mode)格式:  不需要上一级目录就可以创建

os.makedir('/hello/hi')
G:\hello\hi

6.os.remove(path):方法用于删除指定路径的文件。如果指定的路径是一个目录,将抛出OSError。

>>> os.remove('G:\\python_learn\\python_setup\\hi\\name.txt')

7.os.rmkdir():删除目录       os.removedirs(path) :递归删除目录下的文件

>>> os.rmdir('G:\\python_learn\\python_setup\\hi')
>>> os.listdir()
['chromedriver.exe', 'curl.exe', 'DLLs', 'Doc', 'etc', 'geckodriver.exe', 'include', 'Lib', 'Library', 'libs', 'LICENSE.txt', 'NEWS.txt', 'phantomjs-2.1.1-windows', 'phantomjs.exe', 'python-3.7.0-amd64.exe', 'python-3.7.0.exe', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'setup.cfg', 'share', 'tcl', 'Tools', 'twisted', 'vcruntime140.dll', '安装步骤.jpg', '新建文件夹']

 上面基本使我们经常会使用到的关于os模块的方法,后面我还会不断将它的方法补充完整。

see you 

发布了60 篇原创文章 · 获赞 39 · 访问量 3772

猜你喜欢

转载自blog.csdn.net/qq_42992704/article/details/104273086
今日推荐