點贊和關注是我創作的最大動力~~
- os.path.join() 組合路徑生成新路徑
# os.path.join(path, *paths)
# join two or more paths
import os
os.path.join(os.getcwd(),'data') #獲取當前目錄并在其后加'\\data'組合成新的目錄
- glob.glob() 回傳符合某種路徑名模式的路徑串列
# glob.glob(pathname, *, recursive=False)
# Return a list of paths matching a pathname pattern.
import glob
data_path = 'data/train'
glob.glob(os.path.join(data_path, 'image/*.png'))
result:
['data/train\\image\\10.png',
'data/train\\image\\11.png',
'data/train\\image\\12.png',
'data/train\\image\\13.png',
'data/train\\image\\14.png',
'data/train\\image\\15.png',
'data/train\\image\\16.png',
'data/train\\image\\17.png',
'data/train\\image\\18.png',
'data/train\\image\\19.png']
- os.listdir(filepath) 回傳某檔案夾中的檔案名串列;len() 回傳檔案夾中的檔案個數
list_fp = os.listdir(filepath)
# Return a list containing the names of the files in the directory.
len(list_fp) # 回傳該檔案夾中的檔案個數
- str.replace()
str1 = ''
str1.replace()
# Return a copy with all occurrences of substring old replaced by new.
- 路徑格式區分
'\model' # = 'C:\model'
'model' # 程式檔案當前路徑下的model檔案夾
'.\model' # = 'model'
- 移動檔案從原有路徑到新路徑
import shutil # shutil 是對 os 中檔案操作的補充
shutil.move(original_file_path, new_file_path)
- os.path.isdir()
os.path.isdir(filepath)
# Return true if the pathname refers to an existing directory.
- shutil.rmtree()
shutil.rmtree()
# Recursively delete a directory tree
# 即洗掉多級目錄
- os.mkdir() and os.makedirs()
os.mkdir()
# Create a directory
os.makedirs()
# Recursively create a directory tree
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/220994.html
標籤:其他
上一篇:Python影像讀寫方法對比
