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

后端 未结 7 1168
无人共我
无人共我 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条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 20:38

    Just give your desired path if file does not exist earlier;

            from os.path import abspath
            with open ('C:\\Users\\Admin\\Desktop\\results.txt', mode = 'w') as final1:
                print(final1.write('This is my new file.'))
    
            print(f'Text has been processed and saved at {abspath(final1.name)}')
    

    Output will be:

    Text has been processed and saved at C:\Users\Admin\Desktop\results.txt
    

提交回复
热议问题