30:文件系统

一、OS模块中关于文件/目录常用的函数使用方法

getcwd():返回当前工作目录

chdir(path):改变工作目录

listdir(path = '.') :列举制定目录中的文件名('.'表示当前目录,'..'表示上一级目录)

mkdir(path):创建单层目录,如该目录已存在则抛出异常

makedirs(path):递归创建多层目录,如该目录已存在抛出异常,注意:‘E:\\a\\b’和'E:\\a\\c'并不会冲突

remove(path):删除文件

rmdir(path):删除单层目录,·如该目录非空则抛出异常

removedirs(path):递归删除目录,从子目录到父目录逐层尝试删除,遇到目录非空则 异常

rename(old, new):将文件old重命名为new

system(command):运行系统的shell命令,以下是支持路径操作中常用到的一些定义,支持所有平台

                                    os.curdir:指定当前目录('.')

                                    os.pardir:指定上一级目录('..')

                                    os.sep:输出操作系统特定的路径分隔符(Win下为'\\'),Linux下为'/'

                                    os.linesep:当前平台使用的行终止符

                                    os.name:只带当前使用的操作系统

二、使用

1.getcwd()

>>> getcwd()         #获得当前工作目录
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    getcwd()
NameError: name 'getcwd' is not defined
>>> import os
>>> os.getcwd()
'C:\\Program Files\\Python35'

2.chdir()                 #更改工作目录
>>> os.chdir('c:\\')
>>> os.getcwd()
'c:\\'

3.chdir()                 #获得当前工作目录内的所有文件(夹)
>>> os.listdir('c:\\')
['$360Section', '$GetCurrent', '$Recycle.Bin', '360SANDBOX', 'Config.Msi', 'Documents and Settings', 'hiberfil.sys', 'hp', 'inetpub', 'Intel', 'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Recovery', 'swapfile.sys', 'SWSetup', 'System Volume Information', 'SYSTEM.SAV', 'Users', 'Windows', 'Windows.old', 'Windows10Upgrade', 'work', '个人文件', '软件安装包']

4.mkdir()                 #创建单层目录(已存在的目录下可继续创建下一级目录)
>>> os.mkdir('c:\\A')
>>> os.mkdir('c:\\A\\B')
>>> os.mkdir('c:\\C\\D')
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    os.mkdir('c:\\C\\D')
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'c:\\C\\D'

5.makedirs()          #创建多层目录(创建目录已存在则报错)
>>> os.makedirs('c:\\A\\B\\C')
>>> os.makedirs('c:\\A\\B')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    os.makedirs('c:\\A\\B')
  File "C:\Program Files\Python35\lib\os.py", line 241, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'c:\\A\\B'
>>> os.makedirs('c:\\C\\D')

6.remove()  #删除文件  

7.rmdir() #删除目录(目录必须为空)

>>> os.makedirs('c:\\C\\D')
>>> os.makedirs('c:\\A\\B')    # 在c:\\A\\B目录下创建文件test.txt
>>> os.rmdir('c:\\A\\B')
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    os.rmdir('c:\\A\\B')
OSError: [WinError 145] 目录不是空的。: 'c:\\A\\B'
>>> os.remove('c:\\A\\B\\test.txt')
>>> os.rmdir('c:\\A\\B')
>>> os.rmdir('c:\\A')
>>> os.removedirs('c:\\C')
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    os.removedirs('c:\\C')
  File "C:\Program Files\Python35\lib\os.py", line 259, in removedirs
    rmdir(name)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'c:\\C'

8.removedirs()  #逐层删除目录
>>> os.makedirs('c:\\C\\D')
>>> os.removedirs('c:\\C')
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    os.removedirs('c:\\C')
  File "C:\Program Files\Python35\lib\os.py", line 259, in removedirs
    rmdir(name)
OSError: [WinError 145] 目录不是空的。: 'c:\\C'
>>> os.removedirs('c:\\C\\D')

9.os.system(shell命令)

>>> os.system('cmd')    #命令操作符
-1073741510
>>> os.system('calc')    #计算器
0

10.os.curdir        #指定当前目录
>>> os.curdir             
'.'
>>> os.listdir(os.curdir)
['$360Section', '$GetCurrent', '$Recycle.Bin', '360SANDBOX', 'Config.Msi', 'Documents and Settings', 'hiberfil.sys', 'hp', 'inetpub', 'Intel', 'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Recovery', 'swapfile.sys', 'SWSetup', 'System Volume Information', 'SYSTEM.SAV', 'Users', 'Windows', 'Windows.old', 'Windows10Upgrade', 'work', '个人文件', '软件安装包']
 

三、os.path模块

>>> os.path.basename('c:\\A\\B\\C\\test.txt')
'test.txt'


>>> os.path.dirname('c:\\A\\B\\C\\test.txt')
'c:\\A\\B\\C'


>>> os.path.join('c:\\', 'A', 'B')
'c:\\A\\B'
>>> os.path.split('c:\\A\\B\\C\\test.txt')
('c:\\A\\B\\C', 'test.txt')
>>> os.path.split('c:\\A\\B\\C')
('c:\\A\\B', 'C')


>>> os.path.splitext('c:\\A\\B\\C\\test.txt')
('c:\\A\\B\\C\\test', '.txt')


>>> os.path.getatime('c:\\A\\test.txt')
1532351562.5202763


>>> import time
>>> time.gmtime
<built-in function gmtime>
>>> time.gmtime(os.path.getatime('c:\\A\\test.txt'))
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=23, tm_hour=13, tm_min=12, tm_sec=42, tm_wday=0, tm_yday=204, tm_isdst=0)
>>> time.localtime(os.path.getatime('c:\\A\\test.txt'))
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=23, tm_hour=21, tm_min=12, tm_sec=42, tm_wday=0, tm_yday=204, tm_isdst=0)
>>> time.localtime(os.path.getmtime('c:\\A\\test.txt'))
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=23, tm_hour=21, tm_min=15, tm_sec=16, tm_wday=0, tm_yday=204, tm_isdst=0)

>>> os.path.ismount('c:\\')
True
>>> os.path.ismount('c:\\A')
False
>>> 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值