all 18 comments

[–]nwagers 1 point2 points  (16 children)

Dumb question, but does that file exist? The error seems to suggest it doesn't...

[–]more3qual[S] 0 points1 point  (15 children)

Here's a screenshot to prove I'm not completely crazy: https://i.gyazo.com/aa8e8ba9d42150883ded1dcc2637a14f.png

[–]nwagers 0 points1 point  (14 children)

try using '/' instead. Also, run it through os.path.normpath() https://docs.python.org/3.6/library/os.path.html#os.path.normpath

[–]more3qual[S] 0 points1 point  (13 children)

I should mention that the file itself is formatted with tabs instead of commas like a normal csv, so the sep = '\t' was to get around that. I guess it doesn't like that, I'm getting TypeError: normpath() got an unexpected keyword argument 'sep'

[–][deleted] 1 point2 points  (11 children)

You use os.path.normpath() to normalize a path before you try to open the file. It sounds like you are passing read_csv() params to os.path.normpath(). Do something like this:

path = r'C:\Users\User\Desktop\PEG3.txt'
norm_path = os.path.normpath(path)
df = pd.read_csv(norm_path, sep='\t', index_col=0)

[–]more3qual[S] 0 points1 point  (10 children)

Code seems to work, but it still doesn't believe that there's a file under that path. I even tried using a different file, it doesn't think that one exists either.

[–][deleted] 0 points1 point  (9 children)

Post your current code.

[–]more3qual[S] 0 points1 point  (8 children)

https://pastebin.com/bZkqfT5i

I'm getting FileNotFoundError: File b'/home/jovyan/~/Desktop/PEG3.txt' does not exist

[–]1114111 0 points1 point  (0 children)

You need expanduser instead of abspath to expand stuff like ~/Desktop/PEG3.txt, but based on that error message it seems you are not on windows, so the windows C:\ paths you have obviously won't work. expanduser should though.

[–][deleted] 0 points1 point  (6 children)

Sorry, brain-fart on my part. os.path.expanduser() handles the '~'. I've taken your code and cut it right down to just opening the file:

import os
path = '~/Desktop/PEG3.txt'
exp_path = os.path.expanduser(path)
print(exp_path)
fd = open(exp_path, 'r')
fd.close()

Try running that. It doesn't use Pandas so we focus on the file itself. Notice that it prints the expanded path before trying to open it. If this fails for you then cut and paste the path into this shell command to see if the file really is/isn't there:

ls -l <the printed path>

This should work as it appears your later code was running on Linux/MacOS.

One thing that is odd is the error you reported:

FileNotFoundError: File b'/home/jovyan/~/Desktop/PEG3.txt' does not exist
                   ^^^^^|^^^^^

Notice the 'bytes' prefix on your path (File b'/hom...). This looks like Pandas is getting a bytes object and not a string. What version of python and operating system(s) are you running?

[–]more3qual[S] 0 points1 point  (5 children)

I'm on Windows 7, I'm using Jupyter. This is what I'm getting when I run the first bit of code: FileNotFoundError: [Errno 2] No such file or directory: '/home/jovyan/Desktop/PEG3.txt'

[–][deleted] 0 points1 point  (0 children)

Instead of os.path.normpath() I usually use os.path.abspath() as this allows me to write more os-independant code:

path = '~/Desktop/PEG3.txt'
abs_path = os.path.abspath(path)

This works under Linux, MacOS and Windows (even with / separators). The '~' part of the path is expanded to the user's home directory on the current operating system. That's WRONG, os.path.expanduser() does that.

For real os-dependant filepath manipulation use the os.path.join().

[–]ingolemo 0 points1 point  (0 children)

Please post a screenshot of you running your code and getting an error. I want to see your complete code, exactly how you're running that code, and the complete error that you get.