Pin-Jiun / Python

Python Document
0 stars 0 forks source link

30-OS module #30

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago

下方列出幾種 os 模組常用的方法 ( 參考 Python 官方文件:os 多種操作系統接口 ):

方法 | 參數 | 說明 -- | -- | -- getcwd() |   | 取得目前程式的工作資料夾路徑 chdir() | path | 改變程式的工作資料夾路徑 mkdir() | folder | 建立資料夾 rmdir() | folder | 刪除空資料夾 listdir() | folder | 列出資料夾裡的內容 open() | file, mode | 開啟檔案 write() | string | 寫入內容到檔案 rename() | old, new | 重新命名檔案 remove() | file | 刪除檔案 stat() | file | 取得檔案的屬性 close() | file | 關閉檔案 path |   | 取得檔案的各種屬性 system |   | 執行系統命令 ( 等同使用 cmd 或終端機輸入指令 )
print(os.name) # 取得當前的使用平臺,# Windows用'nt'表示,Linux用'posix'表示
print(os.getcwd()) # 取得當前的工作目錄

「./」是代表目前所在目錄 「../」是代表上一層目錄,若目前已經是根目錄則依然為目前所在目錄 「/」在各個目錄名稱之間的分隔符號,如果放置在路徑之前則代表根目錄

os.path.basename(file_path)

去除目錄的路徑,回傳檔案名稱(包含附檔名)

建立目錄,path可以為絕對或相對路徑

建立path目錄(只能建立一級目錄,如'D:\AAA\BBB'),在AAA(已存在)目錄下建立BBB(新增的資料夾)目錄

os.mkdir('D:\AAA\BBB')

建立多級目錄(如'D:\AAA\BBB\CCC\DDD'),可以在D槽下建立AAA目錄, 繼續在AAA目錄下建立BBB目錄,在BBB目錄下建立CCC目錄,在CCC目錄下建立DDD目錄

os.makedirs('D:\AAA\BBB\CCC\DDD')

檢查路徑是否存在

若只是想要查看特定的路徑是否存在,不分檔案或目錄,則可使用 os.path.exists

fileName1 = '../../profile.js' #查詢上上一層路徑是否有profile.js
print(os.path.exists(fileName1))
判斷path是否存在,存在返回True,不存在返回False,output: False

fileName2 = './profile.js' #查詢當層路徑是否有profile.js
print(os.path.exists(fileName2))
output: True
#從一個路徑中提取檔案路徑,去除檔案名稱
os.path.dirname(path)

#取得檔名的絕對路徑
os.path.realpath(path)

#取得檔案的絕對路徑和檔案名稱,路徑和名稱分開
os.path.split(os.path.realpath(path)
import os

print('basename: ' + os.path.basename('/home/user/aaa.txt'))
print('basename: ' + os.path.basename('C:\\User\\Desktop\\aaa.txt'))
basename: /home/user/aaa.txt
basename: aaa.txt

如果取出的檔案名稱不想要包含附檔名的話可以使用 split 切割字串來去除副檔名

unix_path = '/home/user/aaa.txt'
basename = os.path.basename(unix_path).split('.')[0]
print('basename: ' + basename)
basename: aaa

你也可以使用 os.path.splitext() 來達成同樣的效果

import os

unix_path = '/home/user/aaa.txt'
basename = os.path.splitext(os.path.basename(unix_path))[0]
print('basename: ' + basename)

檔案路徑地址拼接

import os
path = os.path.join(os.getcwd(),'gl')

https://shengyu7697.github.io/python-os-path-basename/

Pin-Jiun commented 1 year ago

使用os.walk() 遞迴印出資料夾中所有目錄及檔名

/python/demo/ |-- os_walk.py |-- root | |-- 1file | |-- 2subDir | | |-- 21file | | |-- 22subDir | | | -- 221file | |-- 23file | |-- 3file | -- 3subDir |-- 31file `-- walk.py

4 directories, 8 files


import os

for dirPath, dirNames, fileNames in os.walk("/python/demo/"):
    print dirPath
    for f in fileNames:
        print os.path.join(dirPath, f)

image

Pin-Jiun commented 8 months ago

讀取資料夾底下的檔案名稱

import os

folder_path = "/your/folder/path"

# 列出資料夾中的所有檔案和目錄
files = os.listdir(folder_path)

# 過濾出檔案名稱,排除目錄
file_names = [file for file in files if os.path.isfile(os.path.join(folder_path, file))]

print(file_names)

這裡的 folder_path 是您想要讀取的資料夾路徑。listdir 返回資料夾中所有檔案和目錄的列表,然後使用列表理解式過濾出只包含檔案的部分。

請替換 "/your/folder/path" 為您實際的資料夾路徑。

Pin-Jiun commented 8 months ago
import os

folder_path = "/your/folder/path"

# 列出資料夾中的所有檔案和目錄
files = os.listdir(folder_path)

# 過濾出只有檔案名稱且不帶副檔名的部分
file_names_without_extension = [os.path.splitext(file)[0] for file in files if os.path.isfile(os.path.join(folder_path, file))]

print(file_names_without_extension)