Enzo
Enzo

Reputation: 348

File not found from Python although file exists

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

Upvotes: 0

Views: 29053

Answers (4)

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"

Upvotes: 0

AbdelKh
AbdelKh

Reputation: 542

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.

Upvotes: 0

Koke Cacao
Koke Cacao

Reputation: 476

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.

Upvotes: 1

gonzalo mr
gonzalo mr

Reputation: 164

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.

Upvotes: 6

Related Questions