I am creating a file with the help of python programming language in my hard drive. The code I am using is:
file = open('test.txt','w')
file.write('Blah Blah')
file.close()
This code creates test.txt
file in hard drive but the file is saved in the default location of project. I want to save the file in other location like desktop or somewhere else. Can anyone tell me how to do this.
I am using python 3.5
Thanks in Advance,
Just specify the location in a string variable and then added to the name of file you want to create with doing open
:
import os
my_dir = 'C:\\Test\\My_Dir'
file_name = 'test.txt'
fname = os.path.join(my_dir, file_name)
file = open(fname,'w')
打开文件时需要传递完整的绝对或相对路径:
file = open('/My/Example/Desktop/test.txt','w')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.