[英]How do I loop through directory and save every text file in Python
I have created the following code that reads every text file in a directory.我创建了以下代码来读取目录中的每个文本文件。 When I save the file to lido1 it only saves the last text file in the directory.当我将文件保存到 lido1 时,它只保存目录中的最后一个文本文件。 How do I create a text file for every file in Lido?如何为 Lido 中的每个文件创建一个文本文件?
import os
import uuid
unique = str(uuid.uuid4())
for root, dirs, files in os.walk(r"C:\Users\shane\Desktop\Lido"):
for file in files:
if file.endswith(".txt"):
filename = os.path.join(root, file)
with open(filename) as f:
text = f.read()
file = open('C:\\Users\\shane\\Desktop\\lido1\\'+
unique +'.txt',"w")
file.write(text)
file.close()
import os
import uuid
unique = str(uuid.uuid4())
for root, dirs, files in
os.walk(r"C:\Users\shane\Desktop\Lido"):
for file in files:
if file.endswith(".txt"):
filename = os.path.join(root, file)
unique = str(uuid.uuid4())
with open(filename) as f:
text = f.read()
print(text)
file = open('C:\\Users\\shane\\Desktop\\lido1\\'+ unique +'.txt',"w")
file.write(text)
file.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.