Python 导出具有不同文件名的多个文件
假设我在一个目录中有Python 导出具有不同文件名的多个文件,python,import,export,filenames,Python,Import,Export,Filenames,假设我在一个目录中有n个文件,文件名为:file_1.txt、file_2.txt、file_3.txt…..file_n.txt。我想将它们单独导入Python,然后对它们进行一些计算,然后将结果存储到相应的输出文件中:file\u 1\u o.txt、file\u 2\u o.txt、.file\u n\u o.txt。 我已经了解了如何导入多个文件: import glob import numpy as np path = r'home\...\CurrentDirectory' al
n
个文件,文件名为:file_1.txt、file_2.txt、file_3.txt…..file_n.txt
。我想将它们单独导入Python,然后对它们进行一些计算,然后将结果存储到相应的输出文件中:file\u 1\u o.txt、file\u 2\u o.txt、.file\u n\u o.txt。
我已经了解了如何导入多个文件:
import glob
import numpy as np
path = r'home\...\CurrentDirectory'
allFiles = glob.glob(path + '/*.txt')
for file in allFiles:
# do something to file
...
...
np.savetxt(file, ) ???
不太确定如何在文件名后附加
\u o.txt
(或任何相关字符串),以便输出文件为file\u 1\u o.txt
您可以使用以下代码段构建输出文件名吗
parts = in_filename.split(".")
out_filename = parts[0] + "_o." + parts[1]
其中,我假设in_文件名的格式为“file_1.txt”。当然,最好将
“\u o.”
(扩展名之前的后缀)放在变量中,这样您就可以在一个地方随意更改,并且可以更轻松地更改该后缀。
在你的情况下,这意味着
import glob
import numpy as np
path = r'home\...\CurrentDirectory'
allFiles = glob.glob(path + '/*.txt')
for file in allFiles:
# do something to file
...
parts = file.split(".")
out_filename = parts[0] + "_o." + parts[1]
np.savetxt(out_filename, ) ???
但是您需要小心,因为可能在将文件名传递到np.savetxt
之前,您需要构建完整路径,因此可能需要类似
np.savetxt(os.path.join(path,out\u文件名))
或者类似的东西。
如果您想将更改合并到一行中,并定义“变量中的后缀”,正如我前面提到的,您可以有如下内容
hh = "_o." # variable suffix
..........
# inside your loop now
for file in allFiles:
out_filename = hh.join(file.split("."))
正如@NathanAck在他的回答中提到的那样,它使用另一种方法通过在拆分列表上使用join来完成同样的事情。您可以使用以下代码段来构建输出文件名吗
parts = in_filename.split(".")
out_filename = parts[0] + "_o." + parts[1]
import os
#put the path to the files here
filePath = "C:/stack/codes/"
theFiles = os.listdir(filePath)
for file in theFiles:
#add path name before the file
file = filePath + str(file)
fileToRead = open(file, 'r')
fileData = fileToRead.read()
#DO WORK ON SPECIFIC FILE HERE
#access the file through the fileData variable
fileData = fileData + "\nAdd text or do some other operations"
#change the file name to add _o
fileVar = file.split(".")
newFileName = "_o.".join(fileVar)
#write the file with _o added from the modified data in fileVar
fileToWrite = open(newFileName, 'w')
fileToWrite.write(fileData)
#close open files
fileToWrite.close()
fileToRead.close()
其中,我假设in_文件名的格式为“file_1.txt”。
当然,最好将“\u o.”
(扩展名之前的后缀)放在变量中,这样您就可以在一个地方随意更改,并且可以更轻松地更改该后缀。
在你的情况下,这意味着
import glob
import numpy as np
path = r'home\...\CurrentDirectory'
allFiles = glob.glob(path + '/*.txt')
for file in allFiles:
# do something to file
...
parts = file.split(".")
out_filename = parts[0] + "_o." + parts[1]
np.savetxt(out_filename, ) ???
但是您需要小心,因为可能在将文件名传递到np.savetxt
之前,您需要构建完整路径,因此可能需要类似
np.savetxt(os.path.join(path,out\u文件名))
或者类似的东西。
如果您想将更改合并到一行中,并定义“变量中的后缀”,正如我前面提到的,您可以有如下内容
hh = "_o." # variable suffix
..........
# inside your loop now
for file in allFiles:
out_filename = hh.join(file.split("."))
正如@NathanAck在他的回答中提到的,它使用另一种方法,通过在拆分列表上使用join来做同样的事情
import os
#put the path to the files here
filePath = "C:/stack/codes/"
theFiles = os.listdir(filePath)
for file in theFiles:
#add path name before the file
file = filePath + str(file)
fileToRead = open(file, 'r')
fileData = fileToRead.read()
#DO WORK ON SPECIFIC FILE HERE
#access the file through the fileData variable
fileData = fileData + "\nAdd text or do some other operations"
#change the file name to add _o
fileVar = file.split(".")
newFileName = "_o.".join(fileVar)
#write the file with _o added from the modified data in fileVar
fileToWrite = open(newFileName, 'w')
fileToWrite.write(fileData)
#close open files
fileToWrite.close()
fileToRead.close()