How to create/read a file at a specific location?

+1 Mike Chan · April 10, 2015
How do I make a .txt file in a specific location?
example : fx = open('ex.txt', 'w')
The .txt will be made at where my python file is, so how do I make it build somewhere else?
                  

Post a Reply

Replies

Oldest  Newest  Rating
+2 Colonel Panic · April 10, 2015
Give it the full path

Linux:

 fx = open('/home/scochran/ex.txt', 'w')



That will open the file from my home folder. 

Windows:

 fx = open('C:\Users\scochran\ex.txt', 'w')



That will open it in my windows local users folder. 
0 Mike Chan · April 11, 2015
Ok, i'll try
0 Mike Chan · April 11, 2015
It doesnt work :(, but my friend helped me out,
Code that works : 
import os

f = os.path.join('C:\Users\M!ke\Desktop', 'ex.txt')
fw = open(f, 'a')
  • 1

Python

107,099 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator