簡體   English   中英

如何創建具有不同名稱的多個文件並在 Python 中寫入它們

[英]How to create multiple files with different names and write to them in Python

我在 Python 中有一個 for 循環,我想為每次迭代創建一個新文件。 我希望每個新文件都具有名稱“output.xyz”、“output1.xyz”、“output2.xyz”......等等。 這是我的代碼,它沒有拋出任何錯誤,但沒有寫入任何文件

number_structures = 270
for i in range(number_structures):
    with open('FeCO5.xyz', 'r+') as inputfile:
        molecule = pd.read_table(inputfile, skiprows=2, delim_whitespace=True,
                                 names=['atom', 'x', 'y', 'z'])
        
#Do something with FeC05.xyz number_structures amount of times....
      
           
        with open('output{}.xyz'.format(i), 'ab') as f:
            f.write('atom', 'x', 'y', 'z')

您必須修改以下幾行:

number_structures = 270 
for i in number_structures:

必須變成:

number_structures = 270
for i in range(number_structures):

還:

f.write('atom', 'x', 'y', 'z')

不正確,因為 write 只需要一個參數,但是您給出了 4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM