我只是想从文件中获取一个数字并将其重命名为另一个目录。你能救我吗?
我的代码如下:
path_txt = 'temp'
name_files1 = os.listdir(path_txt)
for TXT in name_files1:
with open(os.path.join(path_txt,TXT), "r") as content:
search = re.search(r'(\d{21,30})|'
r'(\d{7})\-\s*(\d{2})\.(\d{4})\.(\d)\.(\d{2})\.(\d{4})|'
r'(\d{7})\.?(\d{2})\/?(\d{4})\.?(\d)\.?(\d{2})\.?(\d{4})|'
r'(\d{7})\-(\d{2})\-(\d{4})\.(\d)\.(\d{2})\.(\d{4})|'
r'(\d{7})\-(\d{2})\.(\d{4})\.(\d)\.(\d{2})\-(\d{4})', content.read())
text = content.read()
if search is not None:
name1 = search.group(0)
name1 = re.sub(r"\D", "", name1)
fp = os.path.join("20_digitos", name1 + "_%d.txt")
postfix = 0
while os.path.exists(fp % postfix): # Verifica se o arquivo já existe na pasta, se existir incrementa +1 em %d de fp
postfix += 1
os.rename(
os.path.join(path_txt, TXT),
fp % postfix
)
它给了我以下错误:
Traceback (most recent call last):
File "---", line 30, in <module>
fp % postfix
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'temp\\Processo ID 1000000.pdf .txt' -> '20_digitos\\00025087420135020003_0.txt'