1

I'm trying to load a simple text file with an array of numbers into Python. A MWE is

import numpy as np

BASE_FOLDER = 'C:\\path\\'
BASE_NAME = 'DATA.txt'
fname = BASE_FOLDER + BASE_NAME 

data = np.loadtxt(fname)

However, this gives an error while running:

OSError: C:\path\DATA.txt not found.

I'm using VSCode, so in the debug window the link to the path is clickable. And, of course, if I click it the file opens normally, so this tells me that the path is correct.

Also, if I do print(fname), VSCode also gives me a valid path.

Is there anything I'm missing?

EDIT

As per your (very helpful for future reference) comments, I've changed my code using the os module and raw strings:

BASE_FOLDER = r'C:\path_to_folder'
BASE_NAME = r'filename_DATA.txt'
fname = os.path.join(BASE_FOLDER, BASE_NAME) 

Still results in error.

Second EDIT

I've tried again with another file. Very basic path and filename

BASE_FOLDER = r'Z:\Data\Enzo\Waste_Code'
BASE_NAME = r'run3b.txt'

And again, I get the same error. If I try an alternative approach,

os.chdir(BASE_FOLDER)
a = os.listdir()

then select the right file,

fname = a[1]

I still get the error when trying to import it. Even though I'm retrieving it directly from listdir.

>> os.path.isfile(a[1])
False
5
  • Is it an issue with case? I think it should still open it though. Commented Apr 24, 2018 at 11:23
  • 2
    Try with os.path Commented Apr 24, 2018 at 11:24
  • Have you tried to simply open(fname) and read from it? Commented Apr 24, 2018 at 11:25
  • 1
    Try to use raw strings when working with files. Commented Apr 24, 2018 at 11:26
  • @MichaelButscher using the os module tells me that Python can't find the path at all. Though VSCode can... Commented Apr 24, 2018 at 12:51

4 Answers 4

6

Using the module os you can check the existence of the file within python by running

import os
os.path.isfile(fname)

If it returns False, that means that your file doesn't exist in the specified fname. If it returns True, it should be read by np.loadtxt().

Extra: good practice working with files and paths

When working with files it is advisable to use the amazing functionality built in the Base Library, specifically the module os. Where os.path.join() will take care of the joins no matter the operating system you are using.

fname = os.path.join(BASE_FOLDER, BASE_NAME)

In addition it is advisable to use raw strings by adding an r to the beginning of the string. This will be less tedious when writing paths, as it allows you to copy-paste from the navigation bar. It will be something like BASE_FOLDER = r'C:\path'. Note that you don't need to add the latest '\' as os.path.join takes care of it.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the answer, I've updated my code accordingly. Unfortunately it still gives me that error. os.path.isfile(fname) returns False, but the error is the same and the path in the error string is still clickable and active.
In my answer I assumed that path is a variable that stores the actual path to where the file is located. You should check that is pointing to the correct folder.
Just before os.path.isfile(fname) I'm running a print(fname). This gives me a path (absolute path, not relative) in the terminal, which is clickable from VSCode. If I click there, the file opens, so I assume the path is correct.
Unfortunately the exact path contains confidential information itself. I have tried moving the file, though, and it still gives me error. However, if I call the actual filename DATA.txt (The original filenames contain underscores), then the program works. Is there a problem with underscores in filenames?
does the original filename have the format _Data.txt?
|
1

You may not have the full permission to read the downloaded file. Use

sudo chmod -R a+rwx file_name.txt

in the command prompt to give yourself permission to read if you are using Ubuntu.

2 Comments

I have full access to the file. I've created it and it has been working until a few hours ago. I'm under Windows, but I've checked the file properties and I indeed have full permission to the file. Moreover, the problem really is that Python does not "see" the file even though it lists it (see last edit). So it's not a matter of permissions.
also, if you are running the script in your base folder, you don't really need the base folder in your path. like data = np.loadtxt(BASE_NAME)
0

For me the problem was that I was using the Linux home symbol in the link (~/path/file). Replacing it with the absolute path /home/user/etc_path/file worked like charm.

Comments

0

Python interpreter checking the path from which path is there in command prompt while running

like example below

C:/Desktop/python_codes>python code.py

is differ from

C:/Desktop>python "python_codes\code.py"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.