tags:

views:

151

answers:

5

I'm trying to learn how to parse .txt files in Python. This has led me to opening the interpreter (terminal > python) and playing around. However, I can't seem to be able to specify the right path. Where does Python first look?

This is my first step:

    f = open("/Desktop/temp/myfile.txt","file1")

This blatantly doesn't work. Can anyone advise?

+5  A: 

Edit: Oh and yes, your second argument is wrong. Didn't even notice that :)

Python looks where you tell it to for file opening. If you open up the interpreter in /home/malcmcmul then that will be the active directory.

If you specify a path, that's where it looks. Are you sure /Desktop/temp is a valid path? I don't know of many setups where /Desktop is a root folder like that.

Some examples:

  • If I have a file: /home/bartek/file1.txt

  • And I type python to get my interpreter within the directory /home/bartek/

  • This will work and fetch file1.txt ok: f = open("file1.txt", "r")

  • This will not work: f = open("some_other_file.txt", "r") as that file is in another directory of some sort.

  • This will work as long as I specify the correct path: f = open("/home/media/a_real_file.txt", "r")

Bartek
Thanks! I wasn't too sure where it would first look and I didn't fancy specifying every single director! Thanks again!
day_trader
+2  A: 

To begin with, the second argument is the permissions bit: "r" for read, "w" for write, "a" for append. "file1" shouldn't be there.

Brent Newey
+8  A: 

That doesn't work as you've got the wrong syntax for open.

At the interpreter prompt try this:

>>> help(open)
Help on built-in function open in module __builtin__:

open(...)
    open(name[, mode[, buffering]]) -> file object

    Open a file using the file() type, returns a file object.

So the second argument is the open mode. A quick check of the documentation and we try this instead:

f = open("/Desktop/temp/myfile.txt","r")
Dave Webb
+1 for showing the good habits to take, on top of the right answer :-)
RedGlyph
+1  A: 

Try:

f = open('Desktop/temp/myfile.txt', 'r')

This will open file relatively to current directory. You can use '/Desktop/temp/myfile.txt' if you want to open file using absolute path. Second parameter to open function is mode (don't know what file1 should mean in your example).

And regarding the question - Python follows OS scheme - looks in current directory, and if looking for modules, looks in sys.path afterwards. And if you want to open file from some subdirectory use os.path.join, like:

import os
f = open(os.path.join('Desktop', 'temp', 'myfile.txt'), 'r')

Then you're safe from the mess with '/' and '\'.

And see docs for built-in open function for more information about the way to use open function.

Abgan
A: 

This:

import os
os.path

should tell you where python looks first. Of course, if you specify absolute paths (as you have), then this should not matter.

Also, as everyone else has said, your second argument in open is wrong. To find the proper way of doing it, try this code:

help(open)
inspectorG4dget