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
>>>?
?
?
?
?
總結
- 上一篇: 第6章 课后作业
- 下一篇: matlab波导色散,有效折射率法求矩形