Cannot use relative path when using 'Execute Line in Console' in Pycharm - python

I'm using Pycharm for python project. My project structure is like this:
+ project
+ src
- Data.csv
- main.py
This is main.py:
import panda as pd
dataset = pd.read_csv("Data.csv")
When i use 'Execute Line in Console' and run the second line, i get this error:
Traceback (most recent call last):
File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-bd7168d85704>", line 1, in <module>
dataset = pd.read_csv('Data.csv')
File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 429, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 1122, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 1853, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 387, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 705, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'Data.csv' does not exist: b'Data.csv'
When i run the whole code using 'Run', it's fine, so i think that executing in console changes the directory. But i haven't figured out how to fix.

To fix it, just close your current project and open another project which contain your file directly.

Related

Python SLEEPPY demo gets IOError: "sleep_endpoints_summary.csv does not exist"

Full error is:
IOError: File C:\PRIMARY\WORK\wearable\SLEEPPY\RESULTS/demo/sleep_endpoints/sleep_endpoints_summary.csv does not exist
Also: directory SLEEPPY\RESULTS/demo/ is missing.
Is intermixing backslashes and foreslashes a problem?
...and it is missing directory: SLEEPPY\RESULTS/demo/
Directory "C:\PRIMARY\WORK\wearable\SLEEPPY\RESULTS" exists.
I get the IOError when I do "Running the demo files" after I installed https://github.com/elyiorgos/sleeppy sleep measurement.
I'm running on Windows 10 Python 2.7.18
Full output, with error at end...
>>> import sleeppy.tests as demo
>>> demo.run_demo()
Please provide a path to a results directory: C:\PRIMARY\WORK\wearable\SLEEPPY\RESULTS
Loading data...
Error processing: c:\Python27\lib\site-packages\sleeppy\tests\demo.bin
Error: ``C:\PRIMARY\WORK\wearable\SLEEPPY\RESULTS/c:\Python27\lib\site-packages\sleeppy\tests\demo/raw_days/c:\Python27\lib\site-packages\sleeppy\tests`` does not exist
total run time: 6.7197999994 minutes
Checking endpoints...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Python27\lib\site-packages\sleeppy\tests\demo.py", line 37, in run_demo
obtained = collect_endpoints(dst)
File "c:\Python27\lib\site-packages\sleeppy\tests\demo.py", line 43, in collect_endpoints
return pd.read_csv(src).values[0]
File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 787, in __init__
self._make_engine(self.engine)
File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 1708, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
IOError: File C:\PRIMARY\WORK\wearable\SLEEPPY\RESULTS/demo/sleep_endpoints/sleep_endpoints_summary.csv does not exist
>>>
Well, the answer so far is me having to debug github sleeppy for Windows 10.
First, there was init problem with results path.
Next, def split_days_geneactiv_bin crashed with ioerror...

Could someone help solving a Pandas error when saving a csv file with python-requests?

Could someone help me understand why this code thrown an error and provide a solution how I could save my file on my local machine:
myLast = result[:1]
for x in myLast:
urldailyLocal= os.path.basename(x)
s=requests.get(x, verify=False).content
c=pd.read_csv(s)
c.to_csv('path/to/my/file/'+urldailyLocal, index=False)
error after running the above code:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File "/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py", line 676, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py", line 448, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py", line 880, in __init__
self._make_engine(self.engine)
File "/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py", line 1114, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py", line 1891, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 374, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 694, in pandas._libs.parsers.TextReader._setup_parser_source
OSError: Expected file path name or file-like object, got <class 'bytes'> type
myLast list stores an url in such format:
'https://test.com/something/example_2020-09-27-10.51PST_ALL.csv'
The below should work for you
s = requests.get(x, verify=False).content
df = pd.read_csv(io.StringIO(s.decode('utf-8')))

Python : FileNotFoundError: File b'fleet.csv' does not exist

I am getting a FileNotFoundError when I try to read a particular CSV file in the directory.
If i read another CSV file, I can read it properly without any error.
What I have tried
fleet_data=pd.read_csv('data_fleet.csv', sep=',',index_col=0)
fleet_data=pd.read_csv('Users/Ver/Desktop/Processing/data_fleet.csv',sep=',',index_col=0)
fleet_data=pd.read_csv('Users\Ver\Desktop\Processing\data_fleet.csv',sep=',',index_col=0)
fleet_data=pd.read_csv('data_fleet.csv')
I tried changing the name of the file, but it still doesn't work.
Error
fleet_data=pd.read_csv('data_fleet.csv', sep=',',index_col=0)
Traceback (most recent call last):
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\IPython\core\interactiveshell.py", line 2869, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-36-9aab06bbbbcc>", line 1, in <module>
fleet_data=pd.read_csv('data_fleet.csv', sep=',',index_col=0)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\pandas\io\parsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\pandas\io\parsers.py", line 787, in __init__
self._make_engine(self.engine)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\pandas\io\parsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\pandas\io\parsers.py", line 1708, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'data_fleet.csv' does not exist
But
If I copy the CSV file from the project to another project folder and open the Python file and open the file (data_fleet.py), I can read it without any issues.
What is the issue in reading the file in a desired folder?
Try to give it absolute path:
'C:\\Users\\Ver\\Desktop\\Processing\\data_fleet.csv'
Could you try listing the content of your directory? For example using the os module and the listdir() function
>>> import os
>>> contents = os.listdir()
>>> contents
This will let you see if there is any odd characters or something preventing you from "finding it"

I am getting error when opening a CSV file in pycharm

I am using pycharm and when i run a code of opening a csv file using pandas I am getting an error of no existence.
I saved the csv file in my project directory and called it using pandas.
import pandas as pd
df = pd.read_csv("E:\\students")
print(df)
The error when i run the code:
Traceback (most recent call last): File "E:/untitled232/file1.py", line 2, in <module>
df = pd.read_csv("E:\\students") File "E:\untitled232\venv\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds) File "E:\untitled232\venv\lib\site-packages\pandas\io\parsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds) File "E:\untitled232\venv\lib\site-packages\pandas\io\parsers.py", line 787, in __init__
self._make_engine(self.engine) File "E:\untitled232\venv\lib\site-packages\pandas\io\parsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options) File "E:\untitled232\venv\lib\site-packages\pandas\io\parsers.py", line 1708, in __init__
self._reader = parsers.TextReader(src, **kwds) File "pandas\_libs\parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__ File "pandas\_libs\parsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source FileNotFoundError: File b'E:\\students' does not exist
It seems I had to put .csv after the name.

Python: ID error when importing csv file with pandas

I am trying to Import a csv file saved in a local Folder. When I use Anaconda Python Notebook I have no Problems, while using Zeppelin I do have issues.
The code I am using, that works fine in Anaconda, is:
#import csv data
frequency=pd.read_csv("C:\\Users\\L18938\\Desktop\\Vehicle_to_grid\\analysis\\Frequency_March_2018.csv", nrows=86401)
However, when running it on Zeppelin, I receive:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 646, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 389, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 730, in __init__
self._make_engine(self.engine)
File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 923, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 1390, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "pandas/parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4025)
File "pandas/parser.pyx", line 667, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:8031)
IOError: File C:\Users\L18938\Desktop\Vehicle_to_grid\analysis\Frequency_March_2018.csv does not exist
Obviously, the file exists and there are no Errors in the path spelling.
I have tryied / or double \, but nothing changes. Also
os.chdir("C:/Users/L18938/Desktop/Vehicle_to_grid/analysis")
or
os.listdir("C:/Users/L18938/Desktop/Vehicle_to_grid/analysis")
Any idea? thank you in advance
Your Traceback let show you that the python interpreter is running in Unix file path mode (/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py)
When you are under Anaconda, you are in pure windows and your traceback will be something like (C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py)
Anaconda will reach file with a Windows type file-path, and Zeppelin will reach file in a UNIX type file-path.
Your issue is definitely relative to how you specify your path in Zeppelin, you can't use Windows path, but you you may try something like that:
frequency=pd.read_csv("file:///C:/Users/L18938/Desktop/Vehicle_to_grid/analysis/Frequency_March_2018.csv", nrows=86401)

Categories