I am trying to do a simple csv read for my code and it has worked up until I tried tonight for Mac. Currently on 10.15.3, Catalina. This is my code:
df = pd.read_table('/Users/nicholasmori/Desktop/FINAL.csv', delimiter=',')
And the error it gave me was:
OSError: Initializing from file failed.
Ive tried multiple different options to read this csv, including
pd.read.csv(open( ) )
csv.reader( )
pd.read_csv()
with open ( ) as csvfile:
But all these give similar errors. I am sure theres a simple answer, but I haven't been able to find it. Ive tried the
sudo chown username:group filename
command on terminal, and allowed terminal access through my privacy/firewall settings. My file also has Read & Write permissions for "everyone," unless I'm viewing that wrong. Does anyone have a solution for me?
I had the same error, what I did was upload the file to jupyter from my Mac (same root of the python program) and:
import pandas as pd
path_file = "FileName.csv"
data = pd.read_csv(path_file)
Hope works!
When did you upgrade from macOS Catalina (10.15.x) from a previous version (10.14)? OS-level access rights are a bit different.
Also, you can try this, to verify that the file (and path, if provided) are correct:
from pathlib import Path
file_name = 'test.npy'
Path(file_name).is_file() # returns True or False
print(Path.cwd()) # prints current working directory
Related
I am learning Python through 'Automate the Boring Stuff With Python' First Edition. In chapter 12, pg 267, we are supposed to open a file called example.xlsx.
The author's code reads:
import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
type(wb)
However, when I try to open this file, I get the following error (this is the last line of the error):
FileNotFoundError: [Errno 2] No such file or directory: 'example.xlsx'
I know this file exists, because I downloaded it myself and am looking at it right now.
I have tried moving it to the current location in which Python 3.8 is, I have tried saving it with my Automate the Boring Stuff files that I've been working on the desktop, and I have tried saving it in every conceivable location on my machine, but I continue getting this same message.
I have imported openpyxl without error, but when I enter the line
wb = openpyxl.load_workbook('example.xlsx')
I have entered the entire pathway for the example.xlsx in the parenthesis, and I continue to get the same error.
What am I doing wrong? How am I supposed to open an Excel workbook?
I still don't understand how I am doing wrong, but this one is incredibly infuriating, and I feel incredibly stupid, because it must be something simple.
Any insight/help is greatly appreciated.
Your error is unambigous — your file in a supposed directory don't exist. Believe me.
For Python is irrelevant, whether you see it. Python itself must see it.
Specify the full path, using forward slashes, for example:
wb = openpyxl.load_workbook('C:/users/John/example.xlsx')
Or find out your real current (working) directory — and not the one supposed by you — with commands
import os
print(os.getcwd())
then move your example.xlsx to it, and then use only the name of your file
wb = openpyxl.load_workbook('example.xlsx')
You may also verify its existence with commands — use copy/paste from your code to avoid typos in the file name / path
import os.path
print(os.path.exists('example.xlsx')) # True, if Python sees it
or
import os.path
print(os.path.exists('C:/users/John/example.xlsx')) # True, if exists
to be sure that I'm right, i.e. that the error is not in the function openpyxl.load_workbook() itself, but in its parameter (the path to the file) provided by you.
I notice that the extension of the example file is not the same as described in the book, is example.csv. I was facing the same frustration as you
I am in my project folder call "project". I have two neural network h5 file, one in "project/my_folder/my_model_1.h5", I also copy it to folder "project/my_model_2.h5". So I open my Jupyter Notebook which is working at "project" folder.
import h5py
f = h5py.File("my_model_2.h5") # has NO Issue
but
f = h5py.File("my_folder/my_model_1.h5") # OSError
It says OSError: Unable to open file (unable to open file: name = 'my_folder/my_model_1.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
Interestingly, I only have this issue when I do the same thing on my Mac, but I don't encounter any issue in Linux machine.
Please let me know if you know how to fix this. Thank you in advance.
So it looks like some hidden invalid character incidentally got copied when I simply copy and paste the file path from Mac folder system. Take a look at the code in the screen.
The Line 92 is the path name I directly copy and paste from Mac folder.
The Line 93 is the path I literally type with every single letter, then there is no error and .h5 file is loaded properly. It's a kinda of similar issue that has been spotted by someone at this link: Invalid character in identifier
I simply copy the error code to Pycharm, and the unwelcome character got busted.
So solution, for Mac user, be careful of of just simply copying the text from folder system, if something obviously weird, try type every letter into the text editor.
Specifying the absolute path using the os worked in windows
file_name = os.path.dirname(__file__) +'\\my_folder\\my_model_1.h5'
f = h5py.File(file_name)
dont forget to import os though
I made a function that reads from a file and saves it's data in an array.
This function is in Posts.py:
index = 'Forum/Topics/index.txt'
def loadTopicNames():
with open(index, 'r') as file:
data = file.readlines()
for row in data:
row = row.replace('\n', '')
topicNames.append(row)
This function works, doesn't have problems with the file location. But when I import the Posts.py module in my Forum.py module, and execute it from Forum.py, I get this error:
with open(index, 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'Forum/Topics/index.txt'
Here is the relevant code from Forum.py:
import Posts
Posts.loadTopicNames()
Note: I found some solutions on stackoverflow already, but they mostly include making the path absolute, which is not an option here.
Posts.py and Forum.py are in the same folder.
This is sort of a representation of where the files are in the project:
Project/Forum.py
Project/Posts.py
Project/Forum/Topics/index.txt
Project/otherStuff...
Edit: found the problem... The working directory for the Forum.py wasn't right, and that was the reason it messed everything up. It had wrong working directory because when I first made the module, I made it in a wrong folder. Then when I realized my mistake, I just copied it to the right place, but the working directory stayed the same...
This code will work only if you run python Forum.py in the same directory in which the Forum directory lives.
UPD:
I recreated your case on my laptop, and everything works fine. Please check the code:
Posts.py:
index = 'Forum/Topics/index.txt'
def loadTopicNames():
with open(index, 'r') as file:
data = file.readlines()
for row in data:
print(row)
Forum.py:
import Posts
Posts.loadTopicNames()
index.txt:
test text
Project directory:
$ ls -R
Forum Forum.py Posts.py
./Forum:
Topics
./Forum/Topics:
index.txt
Run & output:
$ pwd
/Users/myuser/Forum
$ python Forum.py
test text
You should consider to pass the path to the loadTopic function as a parameter, and passing it on the call in Forum.py
That way you don't have the absolute path hardcoded on the load module.
This code:
workbook.save(outputf)
system('open ' + outputf)
generates this error when run on my mac:
sh: /Users/tylerjw/Desktop/jim_data/August2013_report.xlsx: Permission denied
The file was created with openpyxl. I have not been able to reproduce the error outside of my application is a tkinter application that is writing a considerable amount of data to that file.
When I run similar code in windows it doesn't error, it opens excel with the file. The only difference is the absence of the open command.
What could case this error?
On my system (mac 10.6.8, python2.7.5, gcc 4.2.1) the following code works fine:
from openpyxl import Workbook
from os import system
wb = Workbook()
outputf = 'test.xlsx'
wb.save(outputf)
# see below *
system('open ' + outputf)
(see comments: I lost the bet. The error was somewhere else in the code and has nothing to do with system('open ' + whatever))
I bet there is something wrong with permissions in your new file on your system. Maybe you add
(docu)
st = os.stat(outputf)
os.chmod(outputf, st.st_mode | stat.stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
(with input from other link)
in the code instead of my commentary ('# see below *') Then it should be possible to open it by everyone and everything...
If it works, this is a workaround. There is a difference between our system and I do not know what difference. If not, well, I did not test it on my system (because I did not have a problem with the code), write a comment and I might play with the settings or maybe someone else has an idea.
Btw.: In a terminal in your folder: What output gives 'ls -l excelfilename' after execution of the python code? What programming environment do you use? I start the program via 'python2.7 pythonscript.py' in the terminal.
I'm getting IOError: [Errno 13] Permission denied and I don't know what is wrong wit this code.
I'm trying to read a file given an absolute path (meaning only file.asm),
and a relative path (meaning /.../file.asm), and I want the program to write the file to whatever path is given - if it is absolute, it should write it to the current dir; otherwise, to the path given.
the code:
#call to main function
if __name__ == '__main__':
assem(sys.argv[1])
import sys
def assem(myFile):
from myParser import Parser
import code
from symbolTable import SymbolTable
table=SymbolTable()
# max size of each word
WORD_SIZE = 16
# rom address to save to
rom_addrs = 0
# variable address to save to
var_addrs = 16
# new addition
if (myFile[-4:] == ".asm"):
newFile = myFile[:4]+".hack"
output = open(newFile, 'w') <==== ERROR
the error given:
IOError: [Errno 13] Permission denied: '/Use.hack'
the way I execute the code :
python assembler.py Users/***/Desktop/University/Add.asm
What am I doing wrong here?
Just Close the opened file where you are going to write.
It looks like you're trying to replace the extension with the following code:
if (myFile[-4:] == ".asm"):
newFile = myFile[:4]+".hack"
However, you appear to have the array indexes mixed up. Try the following:
if (myFile[-4:] == ".asm"):
newFile = myFile[:-4]+".hack"
Note the use of -4 instead of just 4 in the second line of code. This explains why your program is trying to create /Use.hack, which is the first four characters of your file name (/Use), with .hack appended to it.
You don't have sufficient permissions to write to the root directory. See the leading slash on the filename?
This happened to me when I was using 'shutil.copyfile' instead of 'shutil.copy'. The permissions were messed up.
I had a same problem. In my case, the user did not have write permission to the destination directory. Following command helped in my case :
chmod 777 University
Maybe You are trying to open folder with open, check it once.
For me nothing from above worked. So I solved my problem with this workaround. Just check that you have added SYSTEM in directory folder. I hope it will help somoene.
import os
# create file
#staticmethod
def create_file(path):
if not os.path.exists(path):
os.system('echo # > {}'.format(path))
# append lines to the file
split_text = text_file.split('\n')
for st in split_text:
os.system('echo {} >> {}'.format(st,path))
Check if you are implementing the code inside a could drive like box, dropbox etc. If you copy the files you are trying to implement to a local folder on your machine you should be able to get rid of the error.
For me, this was a permissions issue.
Use the 'Take Ownership' application on that specific folder.
However, this sometimes seems to work only temporarily and is not a permanent solution.
FYI I had this permission error because the file that it was trying to create was already open/used by another program (was created last time the script was run, I had opened it with excel, and then got a permission error when it was trying to recreate it)
leaving this here in case someone else finds it useful, it is not the real solution to the question asked
I got this error because the directory didn't exist yet.
Solution: create the directory
import os
if not os.path.exists(directory):
os.makedirs(directory)