![](/img/trans.png)
[英]Using a python program to rename all XML files within a linux directory
[英]Rename All Files in a Directory Using Python
我有一個目錄,其中包含許多具有以下格式的文件:
1 or 2 numbers_S followed by 1 or 2 numbers_L001_R1 or R2_001.fastq
Examples: 1_S1_L001_R1_001.fastq or 14_S14_L001_R2_001.fastq
我希望文件名是這樣的: 1_R1.fastq 14_R2.fastq
我已經找到了反映文件名的正則regexp
,並且可以在TextWrangler
成功進行搜索和替換。 以下是我提出的正則表達式:
Search: (\d+)\wS\d+\wL001\w(R\d)\w001(\.fastq)
Replace: \1_\2\3 (or $1_$2$3 depending on the program)
但是,我想知道如何使用簡單的Python腳本批量重命名文件。 我很感激任何建議。
謝謝!
你可以做這樣的事情
import glob, re, os
for filename in glob.glob('/some/dir/*.fastq'):
new_name = re.sub(pattern, r'\1_\2\3', filename)
os.rename(filename, new_name)
考慮使用包os ,從中可以使用os.rename(src,dst) 。 文檔就在這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.