Where does Python first look for files?

I am trying to learn how to parse .txt files in Python. This led me to open an interpreter (terminal> python) and a game. However, it seems I can not indicate the correct path. Where is the first look of Python?

This is my first step:

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

This is clearly not working. Can anyone advise?

+4
source share
7 answers

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

Python looks where you say to open the file. If you open the interpreter in / home / malcmcmul, then this will be the active directory.

If you indicate the path, then where it looks. Are you sure / Desktop / temp is a valid path? I do not know many settings where / Desktop is the root folder.

Some examples:

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

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

  • This will work and extract the file file1.txt in the order: f = open("file1.txt", "r")

  • This will not work: f = open("some_other_file.txt", "r") , because this file is in a different directory.

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

+8
source

This does not work because you have the wrong syntax for open .

At the translator 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 open mode. A quick check of the documentation and we will try this:

 f = open("/Desktop/temp/myfile.txt","r") 
+9
source

To begin with, the second argument is the permission bit: β€œr” for reading, β€œw” for writing, β€œa” for adding. "file1" should not be there.

+2
source

Try:

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

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

And in relation to the question - Python follows the OS scheme - it looks in the current directory, and if it searches for modules, it looks in sys.path after. And if you want to open the file from some subdirectory, use os.path.join, for example:

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

Then you will be safe from clutter with '/' and '\'.

And see the docs for the built-in open function for more information on how to use the open function.

+2
source

A minor potential problem that is not in the original entry, but also make sure the file name argument uses "/" and not "\". This baffled me because the file inspector used the wrong '/' character in its location.

'C: \ Users \ 20 \ Documents \ Projects \ Python \ test.csv' = does not work

'C: /Users/20/Documents/Projects/Python/test.csv' = Works just fine

0
source

Just enter your file name and your details ..... what does it do? β†’ If the path exists, checks whether the file is or not, and then opens and reads

 import os fn=input("enter a filename: ") if os.path.exists(fn): if os.path.isfile(fn): with open(fn,"r") as x: data=x.read() print(data) else: print(fn,"is not a file: ") else: print(fn,"file doesnt exist ") 
0
source

It:

 import os os.path 

should tell you where python looks first. Of course, if you specify the absolute paths (like yours), it does not matter.

Also, like everyone else, your second argument to open is incorrect. To find the right way to do this, try this code:

 help(open) 
-one
source

All Articles