导出具有不同文件名的多个文件
Exporting multiple files with different filenames
假设我在目录中有 n
个文件,文件名:file_1.txt, file_2.txt, file_3.txt .....file_n.txt
。我想将它们单独导入到 Python 中,然后对它们进行一些计算,然后将结果存储到 n
相应的输出文件中:file_1_o.txt, file_2_o.txt, ....file_n_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, ) ???
不太确定如何在文件名后附加 _o.txt
(或与此相关的任何字符串),以便输出文件为 file_1_o.txt
您可以使用以下代码片段构建输出文件名吗?
parts = in_filename.split(".")
out_filename = parts[0] + "_o." + parts[1]
我假设 in_filename 的形式是 "file_1.txt".
当然,将 "_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, ) ???
但你需要小心,因为也许在将 out_filename
传递给 np.savetxt
之前,你需要构建完整路径,因此你可能需要像
这样的东西
np.savetxt(os.path.join(path, out_filename), )
或者类似的东西。
如果您想基本上将更改组合在一行中并定义您的 "suffix in a variable" 正如我之前提到的那样,您可以使用
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()
假设我在目录中有 n
个文件,文件名:file_1.txt, file_2.txt, file_3.txt .....file_n.txt
。我想将它们单独导入到 Python 中,然后对它们进行一些计算,然后将结果存储到 n
相应的输出文件中:file_1_o.txt, file_2_o.txt, ....file_n_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, ) ???
不太确定如何在文件名后附加 _o.txt
(或与此相关的任何字符串),以便输出文件为 file_1_o.txt
您可以使用以下代码片段构建输出文件名吗?
parts = in_filename.split(".")
out_filename = parts[0] + "_o." + parts[1]
我假设 in_filename 的形式是 "file_1.txt".
当然,将 "_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, ) ???
但你需要小心,因为也许在将 out_filename
传递给 np.savetxt
之前,你需要构建完整路径,因此你可能需要像
这样的东西
np.savetxt(os.path.join(path, out_filename), )
或者类似的东西。
如果您想基本上将更改组合在一行中并定义您的 "suffix in a variable" 正如我之前提到的那样,您可以使用
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()