0

I would like to change the folder where a file is created in my Python Script.

Just now I have the next line:

file = open("scraping.txt", "w")

but the problem is that scraping.txt is saved in the root folder and I would like to save it in the same folder where the script is.

How can I do that?

Thanks

Alex
  • 517
  • 4
  • 9
  • 22
  • 1
    Not sure if duplicate or just related: [Change the scripts working directory to the script's own directory](http://stackoverflow.com/questions/1432924/python-change-the-scripts-working-directory-to-the-scripts-own-directory). – Oleh Prypin Apr 05 '14 at 11:30
  • related: [Retrieving python module path](http://stackoverflow.com/q/247770/4279) – jfs Apr 05 '14 at 11:55

4 Answers4

1

Either add the path to the file:

file = open('/path/to/scraping.txt', 'w')

Or use the os module to change the directory:

import os
os.chdir('/path/to/')
file = open('scraping.txt', 'w')
Ben
  • 6,687
  • 2
  • 33
  • 46
1

By default, python creates scraping.txt in the directory from where you invoke the script. In your case, you are invoking the script from root directory thats why file is created at root directory.

you can use os.path.abspath(__file__) for getting folder of your script.

import os
open(os.path.join(os.path.abspath(__file__),'..','scraping.txt'),'w')
venpa
  • 4,268
  • 21
  • 23
  • I have implemented this and it brings me another error in the next line: `file.write("URL\n")` – Alex Apr 05 '14 at 11:36
  • can you please try invoking your script with sudo if you are in unix/Mac? – venpa Apr 05 '14 at 11:39
  • SyntaxError: invalid syntax – Alex Apr 05 '14 at 11:40
  • Ok, now it is working like a charm. The problem was that I was using " instead of ` – Alex Apr 05 '14 at 11:44
  • i have also edited...in winsows dirname is different result when it's executed from different folder. You can use abspath and relative path from there..This should work now. – venpa Apr 05 '14 at 11:46
  • use `os.path.dirname` instead of `..`. – jfs Apr 05 '14 at 11:55
  • i was just getting name of the script folder instead of complete path of script folder in windows if executed from different directory than script...i have initially suggested dirname and later changed to abspath – venpa Apr 05 '14 at 16:50
  • it has nothing to do with `..`; try `open(os.path.join(os.path.dirname(__file__), 'scraping.txt'), 'w')`. Note: `__file__` fails for symlinks, frozen executables, scripts that are run using `execfile()`. Here's how you could [get the script directory in more general case](http://stackoverflow.com/a/22881871/4279) – jfs Apr 05 '14 at 20:39
  • i tried this: print `os.path.dirname( __file__ )` and executed file from different directory: C:\Users\username>python Desktop\test.py; this gives Desktop as output where as i would expect C:\Users\username\Desktop as output. Thanks for letting me know about symlinks; it really helpful.. – venpa Apr 06 '14 at 07:50
0

you just need to add the path :

file = open("/PATH/TO/YOUR/FOLDER/scraping.txt", "w")
jrjc
  • 21,103
  • 9
  • 64
  • 78
  • Is there a way to make it automatically? I mean, that the program detects where the script is located and then write that file on the same folder. – Alex Apr 05 '14 at 11:32
  • you can move first to that directory and then run your script from there, it will create the file in the same place – jrjc Apr 05 '14 at 11:34
  • I am looking for a more straightforward solution such as detecting the directory where the script is located and generate the .txt file in the same directory. – Alex Apr 05 '14 at 11:40
0

By default python creates file in you present working directory. You can know your present working directory using:

import os

os.getcwd()

Just change your directory to the desired one using:

os.chdir('new/directory')

Now create file under your desired path.

Nitin Verma
  • 206
  • 2
  • 14