0

The script stops executing as soon as it hits the open()-method. It is a molecule-writer, that should save the finished formula in a .txt-file. Here are the relevant lines of code:

first_line_form_display = ["  "]

for i in range(9):
    first_line_form_display += ["  "]
    first_line_form_display += [str(i+1)+" "]

form_display = [first_line_form_display]

for i in range(9):
    form_display += [[]]
    for j in range(19):
        form_display[2*i+1] += ["  "]
    form_display += [[str(i+1)+" "]]
    for j in range(18):
        form_display[2*i+2] += ["  "]

molecule_name = input("Enter the name of the molecule: ")

create_molecule = open("Molekuele\\"+molecule_name+".txt","w")
create_molecule.write("")
create_molecule.close()

for i in range(len(form_display)-1):
    save_molecule = open("Molekuele\\"+molecule_name+".txt","a")
    save_molecule.write("  ")
    for j in range(len(form_display[i+1])-1):
        save_molecule.write(form_display[i+1][j+1])
    save_molecule.write("\n")
    save_molecule.close()

I created the program at VS-Code and it worked fine. I did not forget to create the "Molekuele"-folder. It only stops working, when it is not in the VS-Code project folder. The program really stops exactly at the point, where the open()-method is opened, I tested this with several "print("123")'s.

  • 1
    Please provide the error message. – rochard4u Aug 18 '23 at 12:47
  • *It only stops working, when it is not in the VS-Code project folder.* What is the working directory of the process? (You can check by adding `print(os.getcwd())` to the script). A relative path (like "Molekuele/mol.txt") is interpreted relative to that directory. – slothrop Aug 18 '23 at 13:00
  • Does this answer your question? [How to reliably open a file in the same directory as the currently running script](https://stackoverflow.com/questions/4060221/how-to-reliably-open-a-file-in-the-same-directory-as-the-currently-running-scrip) – slothrop Aug 18 '23 at 13:01
  • If you switch to always using forward slashes you don't need to worry about escaping --> `"Molekuele/"+molecule_name+".txt"` even in Windows. Note you can also clean that up with an f-string ---> `f"Molekuele/{molecule_name}.txt`. You might also check out `os.path.join()` to construct a path from a list of strings. – JonSG Aug 18 '23 at 13:06

0 Answers0