NumPy, .txt file not found - python

So here's my code :
climate_data = np.genfromtxt('climate.txt', delimiter = ',', skip_header = 1)
I put this python file in the same directory as the "climate.txt" file and I also already import numpy to my python file
And I get this error :
OSError: climate.txt not found.
What can I do to fix this?

Putting this python script file in the same directory doesn't guarantee your code will be run in the same directory. To make sure of that, use absolute path to climate.txt, or you can get the absolute path to the script directory by writing:
import os
DIR_PATH = os.path.dirname(os.path.abspath(__file__))
climate_data = np.genfromtxt(os.path.join(DIR_PATH, 'climate.txt'), delimiter = ',', skip_header = 1)

Related

How to take full windows file path as user input and work with the file

I am trying to write a program in python that will take a windows file path (uses backslashes not forward slashes) and converts the file to another file type. I think some combination of input() and windows using backslashes is what is causing errors.
For example, the user would input something like
C:\Users\user\Downloads\file.tdms
I want to tell the user to copy the file path from their file explorer and paste it into the program. I cannot alter what the user inputs, it has to be in the above form.
Here is my code thus far:
from nptdms import TdmsFile
from pathlib import Path, PurePosixPath
path = Path(input('enter file path: '))
path1 = PurePosixPath(path)
print(path1)
with open(path1, mode='r') as f:
tdms_file = TdmsFile.read(f)
tdms_file.to_csv(path + '.csv')
Here is the error I get:
(base) H:\Private\ahirani\python TDMS to CSV>testing.py
enter file path: "C:/Users/user/Downloads/file.tdms"
"C:/Users/user/Downloads/file.tdms"
Traceback (most recent call last):
File "H:\Private\ahirani\python TDMS to CSV\testing.py", line 11, in <module>
with open(path, mode='r') as f:
OSError: [Errno 22] Invalid argument: '"C:/Users/user/Downloads/file.tdms"'
It looks like you are using PurePosixPath although your question is specifically about Windows. I would try using WindowsPath and the specific open method on the Path object as listed in the official documentation. Additionally it looks like you have an extra set of double quotes around your input
from nptdms import TdmsFile
from pathlib import WindowsPath
input_path = input('enter file path: ')
path = WindowsPath(input_path.replace('"', ''))
with path.open() as f:
tdms_file = TdmsFile.read(f)
tdms_file.to_csv(path + '.csv')
I don't have immediate access to a Windows Machine to test this but it should work. Also good way to test what your issues are would be to search for your error and try hardcoding it as a debugging step. i.e. take a look at this SO thread

Can't read csv file in same directory [duplicate]

Say I have a Python project that is structured as follows:
project
/data
test.csv
/package
__init__.py
module.py
main.py
__init__.py:
from .module import test
module.py:
import csv
with open("..data/test.csv") as f:
test = [line for line in csv.reader(f)]
main.py:
import package
print(package.test)
When I run main.py I get the following error:
C:\Users\Patrick\Desktop\project>python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
import package
File "C:\Users\Patrick\Desktop\project\package\__init__.py", line 1, in <module>
from .module import test
File "C:\Users\Patrick\Desktop\project\package\module.py", line 3, in <module>
with open("../data/test.csv") as f:
FileNotFoundError: [Errno 2] No such file or directory: '../data/test.csv'
However, if I run module.py from the package directory, I don’t get any errors. So it seems that the relative path used in open(...) is only relative to where the originating file is being run from (i.e __name__ == "__main__")? I don't want to use absolute paths. What are some ways to deal with this?
Relative paths are relative to current working directory.
If you do not want your path to be relative, it must be absolute.
But there is an often used trick to build an absolute path from current script: use its __file__ special attribute:
from pathlib import Path
path = Path(__file__).parent / "../data/test.csv"
with path.open() as f:
test = list(csv.reader(f))
This requires python 3.4+ (for the pathlib module).
If you still need to support older versions, you can get the same result with:
import csv
import os.path
my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, "../data/test.csv")
with open(path) as f:
test = list(csv.reader(f))
[2020 edit: python3.4+ should now be the norm, so I moved the pathlib version inspired by jpyams' comment first]
For Python 3.4+:
import csv
from pathlib import Path
base_path = Path(__file__).parent
file_path = (base_path / "../data/test.csv").resolve()
with open(file_path) as f:
test = [line for line in csv.reader(f)]
This worked for me.
with open('data/test.csv') as f:
My Python version is Python 3.5.2 and the solution proposed in the accepted answer didn't work for me. I've still were given an error
FileNotFoundError: [Errno 2] No such file or directory
when I was running my_script.py from the terminal. Although it worked fine when I run it through Run/Debug Configurations from the PyCharm IDE (PyCharm 2018.3.2 (Community Edition)).
Solution:
instead of using:
my_path = os.path.abspath(os.path.dirname(__file__)) + some_rel_dir_path
as suggested in the accepted answer, I used:
my_path = os.path.abspath(os.path.dirname(os.path.abspath(__file__))) + some_rel_dir_path
Explanation:
Changing os.path.dirname(__file__) to os.path.dirname(os.path.abspath(__file__))
solves the following problem:
When we run our script like that: python3 my_script.py
the __file__ variable has a just a string value of "my_script.py" without path leading to that particular script. That is why method dirname(__file__) returns an empty string "". That is also the reason why my_path = os.path.abspath(os.path.dirname(__file__)) + some_rel_dir_path is actually the same thing as my_path = some_rel_dir_path. Consequently FileNotFoundError: [Errno 2] No such file or directory is given when trying to use open method because there is no directory like "some_rel_dir_path".
Running script from PyCharm IDE Running/Debug Configurations worked because it runs a command python3 /full/path/to/my_script.py (where "/full/path/to" is specified by us in "Working directory" variable in Run/Debug Configurations) instead of justpython3 my_script.py like it is done when we run it from the terminal.
Try
with open(f"{os.path.dirname(sys.argv[0])}/data/test.csv", newline='') as f:
I was surprised when the following code worked.
import os
for file in os.listdir("../FutureBookList"):
if file.endswith(".adoc"):
filename, file_extension = os.path.splitext(file)
print(filename)
print(file_extension)
continue
else:
continue
So, I checked the documentation and it says:
Changed in version 3.6: Accepts a path-like object.
path-like object:
An object representing a file system path. A path-like object is
either a str or...
I did a little more digging and the following also works:
with open("../FutureBookList/file.txt") as file:
data = file.read()

Problems while getting absolute path?

I have the following file which I would like to read, as you can see its incomplete:
file = 'dir2/file.hdf5'
However, I would like to get the full path of file (*):
'/home/user/git_hub_repo/dir1/dir2/file.hdf5'
However, when I do:
from pathlib import Path
filename = Path('dir2/file.hdf5').resolve()
print(filename)
I get:
'/home/user/git_hub_repo/dir2/file.hdf5'
Which is wrong because a dir1 is missing in the retrieved path, how can I get (*) path
Note, that in my terminal i am in:
/home/user/git_hub_repo/
If your current directory is
/home/user/git_hub_repo/
and your file is in
/home/user/git_hub_repo/dir1/dir2/file.hdf5
You should change this
file = 'dir2/file.hdf5'
to
file = 'dir1/dir2/file.hdf5'

Relative file paths in python [duplicate]

Say I have a Python project that is structured as follows:
project
/data
test.csv
/package
__init__.py
module.py
main.py
__init__.py:
from .module import test
module.py:
import csv
with open("..data/test.csv") as f:
test = [line for line in csv.reader(f)]
main.py:
import package
print(package.test)
When I run main.py I get the following error:
C:\Users\Patrick\Desktop\project>python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
import package
File "C:\Users\Patrick\Desktop\project\package\__init__.py", line 1, in <module>
from .module import test
File "C:\Users\Patrick\Desktop\project\package\module.py", line 3, in <module>
with open("../data/test.csv") as f:
FileNotFoundError: [Errno 2] No such file or directory: '../data/test.csv'
However, if I run module.py from the package directory, I don’t get any errors. So it seems that the relative path used in open(...) is only relative to where the originating file is being run from (i.e __name__ == "__main__")? I don't want to use absolute paths. What are some ways to deal with this?
Relative paths are relative to current working directory.
If you do not want your path to be relative, it must be absolute.
But there is an often used trick to build an absolute path from current script: use its __file__ special attribute:
from pathlib import Path
path = Path(__file__).parent / "../data/test.csv"
with path.open() as f:
test = list(csv.reader(f))
This requires python 3.4+ (for the pathlib module).
If you still need to support older versions, you can get the same result with:
import csv
import os.path
my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, "../data/test.csv")
with open(path) as f:
test = list(csv.reader(f))
[2020 edit: python3.4+ should now be the norm, so I moved the pathlib version inspired by jpyams' comment first]
For Python 3.4+:
import csv
from pathlib import Path
base_path = Path(__file__).parent
file_path = (base_path / "../data/test.csv").resolve()
with open(file_path) as f:
test = [line for line in csv.reader(f)]
This worked for me.
with open('data/test.csv') as f:
My Python version is Python 3.5.2 and the solution proposed in the accepted answer didn't work for me. I've still were given an error
FileNotFoundError: [Errno 2] No such file or directory
when I was running my_script.py from the terminal. Although it worked fine when I run it through Run/Debug Configurations from the PyCharm IDE (PyCharm 2018.3.2 (Community Edition)).
Solution:
instead of using:
my_path = os.path.abspath(os.path.dirname(__file__)) + some_rel_dir_path
as suggested in the accepted answer, I used:
my_path = os.path.abspath(os.path.dirname(os.path.abspath(__file__))) + some_rel_dir_path
Explanation:
Changing os.path.dirname(__file__) to os.path.dirname(os.path.abspath(__file__))
solves the following problem:
When we run our script like that: python3 my_script.py
the __file__ variable has a just a string value of "my_script.py" without path leading to that particular script. That is why method dirname(__file__) returns an empty string "". That is also the reason why my_path = os.path.abspath(os.path.dirname(__file__)) + some_rel_dir_path is actually the same thing as my_path = some_rel_dir_path. Consequently FileNotFoundError: [Errno 2] No such file or directory is given when trying to use open method because there is no directory like "some_rel_dir_path".
Running script from PyCharm IDE Running/Debug Configurations worked because it runs a command python3 /full/path/to/my_script.py (where "/full/path/to" is specified by us in "Working directory" variable in Run/Debug Configurations) instead of justpython3 my_script.py like it is done when we run it from the terminal.
Try
with open(f"{os.path.dirname(sys.argv[0])}/data/test.csv", newline='') as f:
I was surprised when the following code worked.
import os
for file in os.listdir("../FutureBookList"):
if file.endswith(".adoc"):
filename, file_extension = os.path.splitext(file)
print(filename)
print(file_extension)
continue
else:
continue
So, I checked the documentation and it says:
Changed in version 3.6: Accepts a path-like object.
path-like object:
An object representing a file system path. A path-like object is
either a str or...
I did a little more digging and the following also works:
with open("../FutureBookList/file.txt") as file:
data = file.read()

How to read the file which is in other directory?

My project
et->datacollector
->eventprocessor->multilang->resources->python->tenderevent->rules->Table.py
->target->inpout->Read.csv
Table.py
import pandas as pd
df_LFB1 = pd.read_csv('Read.csv', sep = ',', usecols = [1,2,7,59])
Now above I want to use Read.csv file how should I give the directory of Read.csv file in pd.read_csv
import os
os.getcwd()
Out[42]: '/Users/Documents'
## os.path.abspath(__file__) ## inside script
If I have the 'Read.csv' file in my current working directory '/Users/Documents', I can read the file like below.
df_LFB1 = pd.read_csv('Read.csv', sep = ',', usecols = [1,2,7,59])
and if my file is not in current working dierctory but in some other directory lets say et directory is in /home/project,
df_LFB1 = pd.read_csv(r'/home/project/et/eventprocessor/target/inpout/ Read.csv',
sep = ',', usecols = [1,2,7,59])
Above statement will read the file.
Note: when you provide absolute path to file. It doesnt not matter where your script resides.

Categories

Resources