日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

30:文件系统

發(fā)布時(shí)間:2023/12/10 windows 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 30:文件系统 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、OS模塊中關(guān)于文件/目錄常用的函數(shù)使用方法

getcwd():返回當(dāng)前工作目錄

chdir(path):改變工作目錄

listdir(path = '.') :列舉制定目錄中的文件名('.'表示當(dāng)前目錄,'..'表示上一級目錄)

mkdir(path):創(chuàng)建單層目錄,如該目錄已存在則拋出異常

makedirs(path):遞歸創(chuàng)建多層目錄,如該目錄已存在拋出異常,注意:‘E:\\a\\b’和'E:\\a\\c'并不會沖突

remove(path):刪除文件

rmdir(path):刪除單層目錄,·如該目錄非空則拋出異常

removedirs(path):遞歸刪除目錄,從子目錄到父目錄逐層嘗試刪除,遇到目錄非空則 異常

rename(old, new):將文件old重命名為new

system(command):運(yùn)行系統(tǒng)的shell命令,以下是支持路徑操作中常用到的一些定義,支持所有平臺

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? os.curdir:指定當(dāng)前目錄('.')

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? os.pardir:指定上一級目錄('..')

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? os.sep:輸出操作系統(tǒng)特定的路徑分隔符(Win下為'\\'),Linux下為'/'

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? os.linesep:當(dāng)前平臺使用的行終止符

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? os.name:只帶當(dāng)前使用的操作系統(tǒng)

二、使用

1.getcwd()

>>> getcwd() ? ? ? ? #獲得當(dāng)前工作目錄
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() ? ? ? ? ? ? ? ??#獲得當(dāng)前工作目錄內(nèi)的所有文件(夾)
>>> 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', '個(gè)人文件', '軟件安裝包']

4.mkdir() ? ? ? ? ? ? ? ??#創(chuàng)建單層目錄(已存在的目錄下可繼續(xù)創(chuàng)建下一級目錄)
>>> 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] 系統(tǒng)找不到指定的路徑。: 'c:\\C\\D'

5.makedirs() ? ? ? ? ?#創(chuàng)建多層目錄(創(chuàng)建目錄已存在則報(bào)錯(cuò))
>>> 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] 當(dāng)文件已存在時(shí),無法創(chuàng)建該文件。: '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目錄下創(chuàng)建文件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] 系統(tǒng)找不到指定的文件。: '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') ? ?#計(jì)算器
0

10.os.curdir ? ? ? ?#指定當(dāng)前目錄
>>> 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', '個(gè)人文件', '軟件安裝包']
?

三、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
>>>?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的30:文件系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。