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 # 獲取當前文件的絕對路徑 dir_1 = os.path.abspath(__file__) # D:\workspace\web-test\Study\Day_5-16\tset3.py # 獲取當前文件所在目錄的上級路徑 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 # 獲取當前文件所在目錄的上級路徑 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 # 獲取當前日期 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 #判斷目錄是否存在,不存在就創建 if not os.path.exists(dir_image_day_pingjie): #創建文件 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") # 創建目錄 # 創建文件 # with open('1.txt',mode='w') as file: # file.write('11111') # os.mknod("n1.txt") # 創建文件,windows上面不支持,linux中支持 url = r "D:\workspace\test36-demo\study\Day_4_13\n2" print ( 'n2目錄下的內容' ,os.listdir(url)) #列出目錄下的所有目錄和文件 # os.rename('1.txt','2.txt') # 文件重命名 print (os.getcwd()) # 獲取當前目錄(絕對路徑 ) # os.rmdir('n1') # 刪除一個空目錄 # os.remove('1.txt') # 刪除一個文件 print ( "文件/目錄是否存在:" ,os.path.exists( '1.txt' )) # 判斷文件/目錄是否存在,結果 True/False print ( '對象是否為目錄:' ,os.path.isdir( '2.txt' )) # 判斷目錄是否存在,是True/否False print ( '對象是否為文件:' ,os.path.isfile( '2.txt' )) # 判斷文件是否存在,是True/否False print ( '文件/目錄的絕對路徑:' ,os.path.abspath( 'n1/n1.txt' )) # 獲取文件/目錄的絕對路徑 print ( '獲取文件的大小:' ,os.path.getsize( 'n1/n1.txt' )) # 獲取文件的大小(單位:b 字節) 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 ( '分離文件名與擴展名:' ,os.path.splitext( 'aa.py' )) # 只會進行分離,不會判斷文件是否真實存在 print ( '分離路徑和文件:' ,os.path.split( 'D:\w1\w2\w3' )) # 只會進行分離,不會判斷目錄及文件是否真實存在 print ( '\n\n--------------------------------作業-----------------------------' ) # 練習一:判斷文件夾是否存在,不存在就創建文件夾,存在就進去,創建一個文件 if os.path.exists( "n1" ) = = False : os.mkdir( 'n1' ) os.chdir( 'n1' ) # 進入目錄 with open ( 'n1.txt' ,mode = 'w' ) as file : file .write( '我的新的' ) # 練習二:獲取n2文件夾下面的所有內容,刪除所有的文件夾 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)) # 是目錄則刪除 # 練習三:自己實現一個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) |
知識點擴展:
Python OS 模塊 文件目錄操作
os模塊中包含了一系列文件操作的函數,這里介紹的是一些在Linux平臺上應用的文件操作函數。由于Linux是C寫的,低層的libc庫和系統調用的接口都是C API,而Python的os模塊中包括了對這寫接口的Python實現,通過Python的os模塊,可以調用系統的功能,進行系統編程。
下面介紹一下os模塊中提供的一些文件操作(僅限Unix平臺):
返回文件對象的操作
os.fdopen(fd, [mode, [bufsize]])
通過文件描述符 fd 創建一個文件對象,并返回這個文件對象
fd參數是一個打開的文件的描述符,在Unix下,描述符是一個小整數。
mode參數是可選的,和buffersize參數和Python內建的open函數一樣,mode參數可以指定‘r,w,a,r+,w+,a+,b'等,表示文件的是只讀的還是可以讀寫的,以及打開文件是以二進制還是文本形式打開。這些參數和C語言中的<stdio.h>中fopen函數中指定的mode參數類似。
bufsize參數是可選的,指定返回的文件對象是否帶緩沖:buffersize=0,表示沒有帶緩沖;bufsize=1,表示該文件對象是行緩沖的;bufsize=正數,表示使用一個指定大小的緩沖沖,單位為byte,但是這個大小不是精確的;bufsize=負數,表示使用一個系統默認大小的緩沖,對于tty字符設備一般是行緩沖,而對于其他文件則一般是全緩沖。如果這個參數沒有制定,則使用系統默認的緩沖設定。
os.popen(command, [mode, [bufsize]])
開啟一個子進程執行一個command指定的命令,在父進程和子進程之間建立一個管道pipe,用于在父子進程間通信。返回一個文件對象,可以對這個文件對象進行讀或寫,取決于參數mode,如果mode指定了只讀,那么只能對文件對象進行讀,如果mode參數指定了只寫,那么只能對文件對象進行寫操作。
command參數指定需要在子進程中執行的命令.
mode參數和bufsize參數和上述的os.fdopen一樣。
os.popen函數還有一些其他的變種,可以按需要使用:
1
|
os.popen2(command, [mode, [bufsize]]) |
在子進程中執行命令command,返回一個二元組(child_stdin, child_stdout)
1
|
os.popen3(command, [mode, [bufsize]]) |
在子進程中執行命令command,返回一個三元組(child_stdin, child_stdout, child_stderr)
1
|
os.popen4(command, [mode, [bufsize]]) |
在子進程中執行命令command,返回一個二元組(child_stdin, child_stdout_and_stderr)
1
|
os.tmpfile() |
返回一個以”w+b“模式打開的文件對象,該文件對象對應的文件無法通過目錄訪問,這是一個臨時文件,當文件對象被關閉的時候,該臨時文件也就被刪除。
到此這篇關于Python關于OS文件目錄處理的實例分享的文章就介紹到這了,更多相關Python OS文件目錄處理內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/Z-Queen/p/14725533.html