Little trouble in open files using python in Mac os - python

I have a CSV file in Documents , and I want to open it with python2.
I run this
print os.getcwd()
/Users/baicai
My file in /Users/baicai/Documents/blabla.csv
I run this get error
df= open('/Documents/blabla.csv')
IOError: [Errno 2] No such file or directory: '/Documents/blala.csv'
or this
f=open('/User/baicai/Documents/blabla.csv')
IOError: [Errno 2] No such file or directory: '/User/baicai/Documents/blabla.csv'
How to read? Thanks

df = open('Documents/blabla.csv') # remove leading /
With the leading /, the operating system thinks you want an absolute path as opposed to a relative path.
or
f=open('/Users/baicai/Documents/blabla.csv') # Users, not User
This one was just a typo :-)

Related

FileNotFoundError: [Errno 2] File b'filepath.csv' does not exist:

I created a df to read a csv in my jupyter notebook library but for some reason the i'm getting an error like this:
FileNotFoundError: [Errno 2] File b'file path/filename.csv' does not exist: b'/file path/filename.csv'
My code is this:
df_csv = pd.read_csv('../file_path/file_name.csv', low_memory=False)
I believe I am using the absolute path to this file...I also checked the directory to confirm I was using the correct path.
import os
print(os.path.abspath('file_name.csv'))
and the output was:
/YXRwLW5vdGVib29rLW5pbmd1eWVuLXNlc3Npb24teGJ1YQ==/file_path/file_name.csv
Is that not the same as '../file_path/file_name.csv'? Would appreciate any troubleshooting tips...
I am not sure why this worked but instead of using the absolute path I used the relative path and just read the file name...A bit concerned with this method since I'd rather be explicit

Permission denied error while merging csv files [duplicate]

This question already has answers here:
PermissionError: [Errno 13] Permission denied
(16 answers)
Closed 6 months ago.
Just starting to learn some Python and I'm having an issue as stated below:
a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')
PermissionError: [Errno 13] Permission denied: 'E:\\Python Win7-64-AMD 3.3\\Test\
Seems to be a file permission error, if any one can shine some light it would be greatly appreciated.
NOTE: not sure how Python and Windows files work but I'm logged in to Windows as Admin and the folder has admin permissions.
I have tried changing .exe properties to run as Admin.
When doing;
a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')
...you're trying to open a directory as a file, which may (and on most non UNIX file systems will) fail.
Your other example though;
a_file = open('E:\Python Win7-64-AMD 3.3\Test\a.txt', encoding='utf-8')
should work well if you just have the permission on a.txt. You may want to use a raw (r-prefixed) string though, to make sure your path does not contain any escape characters like \n that will be translated to special characters.
a_file = open(r'E:\Python Win7-64-AMD 3.3\Test\a.txt', encoding='utf-8')
For me, I was writing to a file that is opened in Excel.
For me, I got this error when I was trying to write a file to a folder and wanted to make sure the folder existed. I accidentally used:
path = Path("path/to/my/file.txt")
path.mkdir(parents=True, exist_ok=True)
with open(path, "w") as file:
...
but the second line means "make a directory at this exact path (and make its parents too, without throwing errors for them existing already)". The third line then throws a PermissionError, because you can't use open() on a directory path, of course! The second line should have been:
path.parent.mkdir(parents=True, exist_ok=True)
I encountered this problem when I accidentally tried running my python module through the command prompt while my working directory was C:\Windows\System32 instead of the usual directory from which I run my python module

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\SUN\\Desktop\\oops in python.txt'

I used a online compiler to access a document in my Desktop using python.
file = open(r"C:\Users\SUN\Desktop\oops in python.txt")
for i in file:
print(i)
file.close()
While, I run this code, I am getting this error
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\SUN\\Desktop\\oops in python.txt'
I Checked the file name and the directory. It's all correct. And I continue to get the same error.
Why am I getting this error?
When I run this code in a Offline Compiler, It works. And I Got the output as required.
It's because the online compiler can't access you system files. The website your are using creates a session for you and runs your code on their servers so you can only access the files you created and saved unto their server during your session.
I think the correct filename is "loops in python.txt" and not "oops in python.txt".
Aren't you missing the l in the beginning?
On windows, you can also use forward slashes (/).
with open ensures the file is closed automatically. i.e.:
with open("C:/Users/SUN/Desktop/loops in python.txt") as f:
for l in f:
...

Exception occurring when trying to open a file with python [duplicate]

This question already has answers here:
How to reliably open a file in the same directory as the currently running script
(9 answers)
Closed 2 years ago.
I am trying to open a file with python like this:
m = open("e.txt", 'r')
The text file I'm trying to open is in the same directory as my python file is.
However I'm getting an error message.
FileNotFoundError: [Errno 2] No such file or directory: 'e.txt'
I've also tried using:
import os
cwd = os.getcwd() # cwd: current working directory
path = os.path.join(cwd, "e.txt")
The error message looks a little different this time:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\e.txt'
I hope someone can help me with this issue. Thanks in advance.
The could be in a folder, if this is the case then the full name has to be written. Such as:
m = open("E://folder/e.txt","r")
print(m.read())
It is important to write the EXACT directory of the file.
The code is perfectly correct. Check the type of file whether it is .txt or some other file. If everything is correct you should check the location you gave

Looping for files in a directory, but cannot open the files in the directory because it doesn't exist

I was trying to read data from a bunch of textfiles in a directory, but getting an error while opening the file
import os
fileList = os.listdir("Desktop/SLUI")
for txtName in fileList:
#Open the textfile
UIname=str(txtName)
userDTL=open(UIname,'r')
if userDTL.mode=='r':
line=userDTL.readlines()
string1=line[0]
string2=line[1]
string3=line[2]
UserDTL.close()
print(string1)
Here is the error when I try to run this code via cmd.exe
File "C:\Users\*****\Desktop\programName.py", line 24, in <module>
userDTL=open(UIname,'r')
FileNotFoundError: [Errno 2] No such file or directory: 'file1.txt'
It's because os.listdir only displays the name of the files, and to open them you need the whole path.
You need to redefine UIname as such:
UIname=os.path.join("Desktop/SLUI",txtName) # I don't think you need the string conversion.
os.path.join() will properly join two (or more) bits of paths, whichever OS you use.
I'm not really familiar with how Python works on Windows, so you might need to replace "Desktop/SLUI" by the appropriate path to your desktop (C:\Users*****\Desktop).

Categories