Python----OS 文件目錄處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import os import time # 獲取當(dāng)前文件的絕對(duì)路徑 dir_1 = os.path.abspath(__file__) # D:\workspace\web-test\Study\Day_5-16\tset3.py # 獲取當(dāng)前文件所在目錄的上級(jí)路徑 dir_2 = os.getcwd() # D:\workspace\web-test\Study\Day_5-16 dir_3_1 = os.path.dirname(dir_1) # D:\workspace\web-test\Study\Day_5-16 dir_3_2 = os.path.dirname(dir_3_1) # D:\workspace\web-test\Study # 獲取當(dāng)前文件所在目錄的上級(jí)路徑 dir_4 = os.path.abspath(os.path.join(os.getcwd(), "./" )) # D:\workspace\web-test\Study\Day_5-16 dir_5 = os.path.abspath(os.path.join(os.getcwd(), "../" )) # D:\workspace\web-test\Study dir_6 = os.path.abspath(os.path.join(os.getcwd(), "../../" )) # D:\workspace\web-test # 獲取當(dāng)前日期 new_time_day = time.strftime( '%Y-%m-%d' ) # 2021-05-21 # 拼接目錄 dir_image_day_pingjie = os.path.join(dir_4,new_time_day) # D:\workspace\web-test\Study\Day_5-16\2021-05-21 #判斷目錄是否存在,不存在就創(chuàng)建 if not os.path.exists(dir_image_day_pingjie): #創(chuàng)建文件 os.mkdir(dir_image_day_pingjie) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import os # os.mkdir("n1") # 創(chuàng)建目錄 # 創(chuàng)建文件 # with open('1.txt',mode='w') as file: # file.write('11111') # os.mknod("n1.txt") # 創(chuàng)建文件,windows上面不支持,linux中支持 url = r "D:\workspace\test36-demo\study\Day_4_13\n2" print ( 'n2目錄下的內(nèi)容' ,os.listdir(url)) #列出目錄下的所有目錄和文件 # os.rename('1.txt','2.txt') # 文件重命名 print (os.getcwd()) # 獲取當(dāng)前目錄(絕對(duì)路徑 ) # os.rmdir('n1') # 刪除一個(gè)空目錄 # os.remove('1.txt') # 刪除一個(gè)文件 print ( "文件/目錄是否存在:" ,os.path.exists( '1.txt' )) # 判斷文件/目錄是否存在,結(jié)果 True/False print ( '對(duì)象是否為目錄:' ,os.path.isdir( '2.txt' )) # 判斷目錄是否存在,是True/否False print ( '對(duì)象是否為文件:' ,os.path.isfile( '2.txt' )) # 判斷文件是否存在,是True/否False print ( '文件/目錄的絕對(duì)路徑:' ,os.path.abspath( 'n1/n1.txt' )) # 獲取文件/目錄的絕對(duì)路徑 print ( '獲取文件的大小:' ,os.path.getsize( 'n1/n1.txt' )) # 獲取文件的大小(單位:b 字節(jié)) url_name = r "D:\workspace\test36-demo\study\Day_4_13\n1\n1.txt" name = os.path.basename(url) # 獲取文件名/文件夾的名稱 dir = os.path.dirname(url) # 獲取文件/文件夾的路徑 print ( 'dir---->' , dir , '\t\t\t' , 'name---->' ,name) print ( '分離文件名與擴(kuò)展名:' ,os.path.splitext( 'aa.py' )) # 只會(huì)進(jìn)行分離,不會(huì)判斷文件是否真實(shí)存在 print ( '分離路徑和文件:' ,os.path.split( 'D:\w1\w2\w3' )) # 只會(huì)進(jìn)行分離,不會(huì)判斷目錄及文件是否真實(shí)存在 print ( '\n\n--------------------------------作業(yè)-----------------------------' ) # 練習(xí)一:判斷文件夾是否存在,不存在就創(chuàng)建文件夾,存在就進(jìn)去,創(chuàng)建一個(gè)文件 if os.path.exists( "n1" ) = = False : os.mkdir( 'n1' ) os.chdir( 'n1' ) # 進(jìn)入目錄 with open ( 'n1.txt' ,mode = 'w' ) as file : file .write( '我的新的' ) # 練習(xí)二:獲取n2文件夾下面的所有內(nèi)容,刪除所有的文件夾 url2 = r "D:\workspace\test36-demo\study\Day_4_13\n2" data = os.listdir(url2) #列出目錄下的所有目錄和文件 for i in data: if os.path.isdir(os.path.join(url2,i)) = = True : # 判斷是否是目錄 os.rmdir(os.path.join(url2,i)) # 是目錄則刪除 # 練習(xí)三:自己實(shí)現(xiàn)一個(gè)os.path.split 分離目錄與文件夾 url_name = r "D:\workspace\test36-demo\study\Day_4_13\n1" name = os.path.basename(url) # 獲取文件名/文件夾的名稱 dir = os.path.dirname(url) # 獲取文件/文件夾的路徑 print ( '目錄---->' , dir , '\t\t\t' , '文件夾---->' ,name) |
知識(shí)點(diǎn)擴(kuò)展:
Python OS 模塊 文件目錄操作
os模塊中包含了一系列文件操作的函數(shù),這里介紹的是一些在Linux平臺(tái)上應(yīng)用的文件操作函數(shù)。由于Linux是C寫的,低層的libc庫(kù)和系統(tǒng)調(diào)用的接口都是C API,而Python的os模塊中包括了對(duì)這寫接口的Python實(shí)現(xiàn),通過Python的os模塊,可以調(diào)用系統(tǒng)的功能,進(jìn)行系統(tǒng)編程。
下面介紹一下os模塊中提供的一些文件操作(僅限Unix平臺(tái)):
返回文件對(duì)象的操作
os.fdopen(fd, [mode, [bufsize]])
通過文件描述符 fd 創(chuàng)建一個(gè)文件對(duì)象,并返回這個(gè)文件對(duì)象
fd參數(shù)是一個(gè)打開的文件的描述符,在Unix下,描述符是一個(gè)小整數(shù)。
mode參數(shù)是可選的,和buffersize參數(shù)和Python內(nèi)建的open函數(shù)一樣,mode參數(shù)可以指定‘r,w,a,r+,w+,a+,b'等,表示文件的是只讀的還是可以讀寫的,以及打開文件是以二進(jìn)制還是文本形式打開。這些參數(shù)和C語言中的<stdio.h>中fopen函數(shù)中指定的mode參數(shù)類似。
bufsize參數(shù)是可選的,指定返回的文件對(duì)象是否帶緩沖:buffersize=0,表示沒有帶緩沖;bufsize=1,表示該文件對(duì)象是行緩沖的;bufsize=正數(shù),表示使用一個(gè)指定大小的緩沖沖,單位為byte,但是這個(gè)大小不是精確的;bufsize=負(fù)數(shù),表示使用一個(gè)系統(tǒng)默認(rèn)大小的緩沖,對(duì)于tty字符設(shè)備一般是行緩沖,而對(duì)于其他文件則一般是全緩沖。如果這個(gè)參數(shù)沒有制定,則使用系統(tǒng)默認(rèn)的緩沖設(shè)定。
os.popen(command, [mode, [bufsize]])
開啟一個(gè)子進(jìn)程執(zhí)行一個(gè)command指定的命令,在父進(jìn)程和子進(jìn)程之間建立一個(gè)管道pipe,用于在父子進(jìn)程間通信。返回一個(gè)文件對(duì)象,可以對(duì)這個(gè)文件對(duì)象進(jìn)行讀或?qū)懀Q于參數(shù)mode,如果mode指定了只讀,那么只能對(duì)文件對(duì)象進(jìn)行讀,如果mode參數(shù)指定了只寫,那么只能對(duì)文件對(duì)象進(jìn)行寫操作。
command參數(shù)指定需要在子進(jìn)程中執(zhí)行的命令.
mode參數(shù)和bufsize參數(shù)和上述的os.fdopen一樣。
os.popen函數(shù)還有一些其他的變種,可以按需要使用:
1
|
os.popen2(command, [mode, [bufsize]]) |
在子進(jìn)程中執(zhí)行命令command,返回一個(gè)二元組(child_stdin, child_stdout)
1
|
os.popen3(command, [mode, [bufsize]]) |
在子進(jìn)程中執(zhí)行命令command,返回一個(gè)三元組(child_stdin, child_stdout, child_stderr)
1
|
os.popen4(command, [mode, [bufsize]]) |
在子進(jìn)程中執(zhí)行命令command,返回一個(gè)二元組(child_stdin, child_stdout_and_stderr)
1
|
os.tmpfile() |
返回一個(gè)以”w+b“模式打開的文件對(duì)象,該文件對(duì)象對(duì)應(yīng)的文件無法通過目錄訪問,這是一個(gè)臨時(shí)文件,當(dāng)文件對(duì)象被關(guān)閉的時(shí)候,該臨時(shí)文件也就被刪除。
到此這篇關(guān)于Python關(guān)于OS文件目錄處理的實(shí)例分享的文章就介紹到這了,更多相關(guān)Python OS文件目錄處理內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/Z-Queen/p/14725533.html