私はPythonを学習していて、英語もしています。そして私は簡単かもしれない問題を抱えていますが、解決できません。私は.TXT年代のフォルダを持っている 、私は各one.Iの17個の数字のシーケンスは、私が.txtの名前を変更したときに空のファイルを作成する
import os
import re
path_txt = (r'C:\Users\usuario\Desktop\files')
name_files = os.listdir(path_txt)
for TXT in name_files:
with open(path_txt + '\\' + TXT, "r") as content:
search = re.search(r'(\d{5}\.?\d{4}\.?\d{3}\.?\d{2}\.?\d{2}\-?\d)', content.read())
if search is not None:
print(search.group(0))
f = open(os.path.join("Processes" , search.group(0) + ".txt"), "w")
for line in content:
print(line)
f.write(line)
f.close()
その作成から抽出されたシーケンスで、各ファイルの名前を変更する必要があり、正規表現で抽出することができました。 txtは "Processes"フォルダには空ですが、必要な名前を付けました。
PS:Pythonの3
を 'open'が与えられた名前で*新しいファイル*を作成します、それはあなたのファイルの名前を変更しません。あなたは['os.rename'](https://stackoverflow.com/questions/2759067/rename-multiple-files-in-a-directory-in-python)を見てください。ちょっとした調整が必要です現時点では得られている。 – hnefatl