I saved csv file in (C:/Users/kgu/PycharmProjects/untitled1/test1.py). named as testdata.csv
my code is :
import csv
f=open('testdata.csv','r',encoding='utf-8')
And the error message is here :
C:\Users\kgu\Anaconda3\python.exe
C:/Users/kgu/PycharmProjects/untitled1/test1.py
Traceback (most recent call last):
File "C:/Users/kgu/PycharmProjects/untitled1/test1.py", line 3, in <module>
f=open('testdata.csv','r',encoding='utf-8')
FileNotFoundError: [Errno 2] No such file or directory: 'testdata.csv'
Process finished with exit code 1
I did save the testdata.csv in the python project folder.
I don't know what is wrong in it.
If you use Pandas, you can try:
import pandas as pd
filepath = "/yourfilepath/yourfile.csv"
# check your sep (";" , "," ...)
f = pd.read_csv(filepath, encoding="utf-8", sep="yoursep")
You could do it like this - if you are in the right working directory:
with open(testdata.csv', 'rb') as csvfile:
data = [x for x in csv.reader(csvfile, delimiter='yourdelimiter')]
Related
In a Folder called Assignment Parser, I've my parsing.py file along with a auth.txt file. Trying to open this auth.txt file. But getting an error that says :
(base) C:\Users\Ajay\Desktop\Python\Assignment Parser>python parsing.py
Traceback (most recent call last):
File "parsing.py", line 27, in <module>
main()
File "parsing.py", line 8, in main
file = open(file_path / "auth.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Ajay\\Desktop\\Python\\Assignment Parser\\auth.txt'
Code:
from pathlib import Path
import os
def main():
# read file
# C:\Users\Ajay\Desktop\Python\Assignment Parser\
file_path = Path("C:/Users/Ajay/Desktop/Python/Assignment Parser/")
file = open(file_path / "auth.txt","r")
# file = open("auth.txt", "r")
lines = file.readlines()
file.close()
Where is this going wrong? PFA for the screenprint.
Try this:
from pathlib import Path
import os
def main():
# read file
# C:\Users\Ajay\Desktop\Python\Assignment Parser\
file_path = Path("C:/Users/Ajay/Desktop/Python/Assignment Parser/")
file = open(os.path.join(file_path, "auth.txt"), "r")
# file = open("auth.txt", "r")
lines = file.readlines()
file.close()
I think the problem is in file extension, I see parsing has .py extension but auth is not
please try file = open(file_path / "auth", "r") again (just delete .txt extension)
As you have your python file in same folder as your text file. You can directly use below code.
def main():
file = open("./auth.txt")
lines = file.readlines()
file.close()
Also make sure, your syder working directory is set to this folder path "C:/Users/Ajay/Desktop/Python/Assignment Parser"
This question already has answers here:
Python raising FileNotFoundError for file name returned by os.listdir
(3 answers)
Closed 3 years ago.
I am trying to read a CSV file. I have two CSVs under two folders. I get the CSV file but when I try to read the content, it says that I dont have such file or directory.
import os
import csv
def scanCSVFile(folder, output_file_name):
myfile = open(output_file_name, "a")
files = os.listdir(folder)
for file in files:
if file.endswith('.csv'): #gives two csv files
with open(file) as csvfile:
csvreader = csv.reader(csvfile)
next(csvreader)
for line in csvreader:
print (line)
myfile.close()
def openCSV(path):
scanCSVFile("./src/goi","goi.yml")
scanCSVFile("./src/kanji","kanji.yml")
openCSV('.')
Error:
C:\Users\Peace\Documents\LMS>python csvToYaml.py
Traceback (most recent call last):
File "csvToYaml.py", line 26, in <module> openCSV('.')
File "csvToYaml.py", line 24, in openCSV scanCSVFile("./src/goi","goi.yml")
File "csvToYaml.py", line 14, in scanCSVFile with open(file) as csvfile:
FileNotFoundError: [Errno 2] No such file or directory: 'Pro.csv'
You have to delete break from code
import os,glob
folder_path = r'path'
for filename in glob.glob(os.path.join(folder_path, '*.csv')):
with open(filename, 'r') as f:
for line in f:
print(line)
You can read data from csv file using pandas.Data will be stored in dataframes.
".yml" extension is obsolete now. Mostly ".yaml" is used.
The below link shows how to read .yaml files.
How can I parse a YAML file in Python
# loading libraries and reading the data from csv files.
import pandas as pd
market_df = pd.read_csv("./src/goi.csv")
print(market_df)
I'm getting a file not found error in JupyterLab. I can see the file and I have tried several different ways to open it via JupyterLab, but I keep getting an error as if the file does not exist.
Below is my code:
import os
import csv
csvpath = os.path.join('..','Resources','budget_data.csv')
with open(csvpath, newline="") as csvfile:
`enter code here`csvreader = csv.reader(csvfile, delimiter=",")
Here is the error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-17-3c32e2cc9d43> in <module>
----> 1 with open(csvpath, newline="") as csvfile:
2 csvreader = csv.reader(csvfile, delimiter=",")
FileNotFoundError: [Errno 2] No such file or
directory: '..,Resources,budget_data.csv'
Here is the path: GTATL201902DATA3/03-Python/Homework/Instructions/PyBank/Resources/budget_data.csv
csvpath = os.path.join('..','Resources','budget_data.csv')
Are you absolutely sure this is the actual code you're using? Because going by the error message, it looks like you did this instead:
csvpath = os.path.join('..,Resources,budget_data.csv')
i.e. the .. and the commas are literally part of the string, instead of joining three separate strings.
I have the following code:
for file in os.listdir('/home/sainik/Final/'+str(folderno)):
if file.endswith('.csv'):
print file
with open(file,'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
print row
when running the code, I am getting the following error:
Traceback (most recent call last):
File "/home/sainik/Final/Programs/sainik.py", line 28, in <module>
with open(file,'rb') as csvfile:
IOError: [Errno 2] No such file or directory: '4.csv'
Kindly help.
You are trying to open the file from the path you are running the script.
You should try to open the full path
with open('/home/sainik/Final/' + file)
You are passing only the filename for open function. You should pass path to the open function. Two possible ways to pass the path of file to open function, either relative path or full path.
try:
with open( os.path.join('/home/sainik/Final/',str(folderno),file),'rb') as csvfile:
Your script is looking at it's own directory for file 4.csv.
Try it like this:
for file in os.listdir('/home/sainik/Final/'+str(folderno)):
if file.endswith('.csv'):
print file
with open(/home/sainik/Final/'+str(folderno)+'\/'+file,'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
print row
So, I'm trying to unzip a .jar file using this code:
It won't unzip, only 20 / 500 files, and no folders/pictures
The same thing happens when I enter a .zip file in filename.
Any one any suggestions?
import zipfile
zfilename = "PhotoVieuwer.jar"
if zipfile.is_zipfile(zfilename):
print "%s is a valid zip file" % zfilename
else:
print "%s is not a valid zip file" % zfilename
print '-'*40
zfile = zipfile.ZipFile( zfilename, "r" )
zfile.printdir()
print '-'*40
for info in zfile.infolist():
fname = info.filename
data = zfile.read(fname)
if fname.endswith(".txt"):
print "These are the contents of %s:" % fname
print data
filename = fname
fout = open(filename, "w")
fout.write(data)
fout.close()
print "New file created --> %s" % filename
print '-'*40
But, it doesn't work, it unzips maybe 10 out of 500 files
Can anyone help me on fixing this?
Already Thanks!
I tried adding, what Python told me, I got this:
Oops! Your edit couldn't be submitted because:
body is limited to 30000 characters; you entered 153562
and only the error is :
Traceback (most recent call last):
File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'
The files that get unzipped:
amw.Class
amx.Class
amz.Class
ana.Class
ane.Class
anf.Class
ang.Class
ank.Class
anm.Class
ann.Class
ano.Class
anq.Class
anr.Class
anx.Class
any.Class
anz.Class
aob.Class
aoc.Class
aod.Class
aoe.Class
This traceback tells you what you need to know:
Traceback (most recent call last):
File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'
The error message says that either the file ClientBrandRetriever.class doesn't exist or the directory net/minecraft/client does not exist. When a file is opened for writing Python creates it, so it can't be a problem that the file does not exist. It must be the case that the directory does not exist.
Consider that this works
>>> open('temp.txt', 'w')
<open file 'temp.txt', mode 'w' at 0x015FF0D0>
but this doesn't, giving nearly identical traceback to the one you are getting:
>>> open('bogus/temp.txt', 'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'bogus/temp.txt'
Creating the directory fixes it:
>>> os.makedirs('bogus')
>>> open('bogus/temp.txt', 'w')
<open file 'bogus/temp.txt', mode 'w' at 0x01625D30>
Just prior to opening the file you should check if the directory exists and create it if necessary.
So to solve your problem, replace this
fout = open(filename, 'w')
with this
head, tail = os.path.split(filename) # isolate directory name
if not os.path.exists(head): # see if it exists
os.makedirs(head) # if not, create it
fout = open(filename, 'w')
If python -mzipfile -e PhotoVieuwer.jar dest works then you could:
import zipfile
with zipfile.ZipFile("PhotoVieuwer.jar") as z:
z.extractall()