我想通過以 .txt 結尾的檔案重命名,這些檔案深深地嵌套在一組檔案中,基于目錄的名稱,但只取“_”之前的那個,因為它是 ID,從而創建“ID_TreatedSubject.txt “ 檔案
這是我必須開始的:(這是一個更長的串列,僅供參考)
/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)/TreatedSubject.txt
/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Kidney, RMA)/TreatedSubject.txt
期望的輸出:
/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)/4BA_TreatedSubject.txt
/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Kidney, RMA)/4BA_TreatedSubject.txt
這是嘗試:
import os
def list_files(dir):
sub_folders = os.listdir(dir)
# print(sub_folders)
for sub_folder in sub_folders:
sub_folder_path = os.path.join(dir,sub_folder)
for root, dirs, files in os.walk(sub_folder_path):
# print(type(root))
for file in files:
if file.endswith(".txt"):
a_string = root
partitioned_string = a_string.partition('_')
print(partitioned_string)
root = before_first_period = partitioned_string[0]
new_filename = sub_folder file
# print(new_filename)
os.rename(os.path.join(root, file), os.path.join(root, new_filename))
input_dir = "/Users/Owner/Desktop/test/Blood2/"
assert os.path.isdir(input_dir),"Enter a valid directory path which consists of sub-directories"
list_files(input_dir)
這是我得到的錯誤:
('/Users/Owner/Desktop/test/Blood2/4BA', '_', 'Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-128-53f8924d30fa> in <module>
19 input_dir = "/Users/Owner/Desktop/test/Blood2/"
20 assert os.path.isdir(input_dir),"Enter a valid directory path which consists of sub-directories"
---> 21 list_files(input_dir)
<ipython-input-128-53f8924d30fa> in list_files(dir)
16 new_filename = sub_folder file
17 # print(new_filename)
---> 18 os.rename(os.path.join(root, file), os.path.join(root, new_filename))
19 input_dir = "/Users/Owner/Desktop/test/Blood2/"
20 assert os.path.isdir(input_dir),"Enter a valid directory path which consists of sub-directories"
FileNotFoundError: [Errno 2] No such file or directory: '/Users/Owner/Desktop/test/Blood2/4BA/TreatedSubject.txt' -> '/Users/Owner/Desktop/test/Blood2/4BA/4BA_Kidney_Blood_CEL-archiveTreatedSubject.txt'
如果我洗掉此塊:
a_string = root
partitioned_string = a_string.partition('_')
print(partitioned_string)
root = before_first_period = partitioned_string[0]
我得到以下輸出:
/Users/Owner/Desktop/test/Blood2/4BA_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)/4BA_Kidney_Blood_CEL-archiveTreatedSubject.txt
/Users/Owner/Desktop/test/Blood2/4BA_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Kidney, RMA)/4BA_Kidney_Blood_CEL-archiveTreatedSubject.txt
我只需要它從“_”中拆分出來,而不是添加整行。任何幫助將不勝感激,我覺得我很接近!
uj5u.com熱心網友回復:
os.walk將使這變得更簡單。
如果我理解這一點,ID 是在第一個_和它的前進/路徑分隔符之間。所以我們可以在使用(也可以).split('/')獲取字串的相關部分后使用.partition.split('_', 1)
因為os.walk已經將路徑與檔案名分開了,所以我們可以通過在現有檔案名前面加上 ID 和下劃線來構造一個新的檔案名。
import os
def get_id(fp):
return fp.partition('_')[0].split('/')[-1]
def rename_files(root_dir):
for root, dirs, files in os.walk(root_dir):
for fname in files:
if fname.endswith('.txt'):
fp = os.path.join(root, fname)
id_ = get_id(fp)
new_fname = f'{id_}_{fname}'
new_fp = os.path.join(root, new_fname)
print('renaming', fp, 'to', new_fp)
# os.rename(fp, new_fp)
rename_files('/Users/Owner/Desktop/test/')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/436481.html
