I would need a script that reads information in one file and reports it in an order I indicated in another text file.
With simple input/output statements I managed to do it but the problem is that I need this to be done for all the files in the directory and that each output produced has the same name as the file from which it comes.
Could someone help me?
I am new to Python but I have to learn quickly for my master’s thesis work.
Build a function that can process one file. Then iterate over every file in a directory (look at
os.listdir() for that part) and call your function once for each file. Putting them into a different directory should be easy.
If that hint isn’t enough to get you started, post your code and we can help further.
Good morning, thanks for the help.
I tried to do as you told me but I couldn’t.
I describe the problem in more detail.
I have a directory with about 7000 files with the .xyz extension that consist of a variable number of rows. I have to discard the second line and copy the remaining ones into a new file with different extension by adding text and converting all the letters ‘H’ of the lines to ‘1’.
I would also like each new .txt file to have the same name as the .xyz file from which it was generated and to be saved in a different directory.
This is the following working code but it is for only one file at a time:
c_input=open('C:/.../prova_input.txt', 'w') c_input.write('MOLECULE') c_input.write('\n') c_input.write('1') c_input.write('\n') file_to_read=open('C:/.../Water_1.xyz', 'r') a=file_to_read.readline() c_input.write(a) lista=file_leggere.readlines() for i in range(len(lista)): if i>0: c_input.write((lista[i])) file_to_read.close() c_input.close() c_input=open('C:/.../prova_input.txt', 'r') file_source=c_input.read() c_input.close() c_input=open('C:/.../prova_input.txt', 'w') replace_string=file_source.replace('H', '1') c_input.write(replace_string) c_input.close()