Telling Python to save a .txt file to a certain directory on Windows and Mac

后端 未结 7 1165
无人共我
无人共我 2020-11-28 19:55

How do you tell Python where to save a text file?

For example, my computer is running the Python file off my desktop. I want it to save all the text file in my docum

7条回答
  •  -上瘾入骨i
    2020-11-28 20:22

    Using an absolute or relative string as the filename.

    name_of_file = input("What is the name of the file: ")
    completeName = '/home/user/Documents'+ name_of_file + ".txt"
    file1 = open(completeName , "w")
    toFile = input("Write what you want into the field")
    file1.write(toFile)
    file1.close()
    

提交回复
热议问题