file not found error
long time no see. im trying to do some homework for a cis class im taking at my community college. i need to open a file and its saying file not found. another bull fn ish error that i cant figure out. keeps saying file not found.. i have the .py file in the same folder as the .csv and i have copied and pasted the file name to avoid any misspellings. any suggestions on what else can be the issue..
@resserone13 Personnally, I use this kind of code to analyze .csv file
import csv from io import StringIO with open('xxxx.csv', mode=r'rb') as fil: b = fil.read() # file in bytes b = b.decode() # bytes -> str i_lf = 0 j_lf = b.find('\r\n',i_lf) while i_lf < (len(b)-1): recinp = b[i_lf:j_lf] #print(recinp) i_lf = j_lf + 2 # 1er char after \n (\r\n = 2 chars) j_lf = b.find('\r\n',i_lf) if j_lf == -1: j_lf = len(b) # simulate \n after last char # use csv module to split only one string flds_lines = csv.reader(StringIO(recinp),delimiter=',',quotechar='"') for fldsinp in flds_lines: print(fldsinp)
@cvp im laughing so hard right now. if i turned your code in the teach would be wanting to learn from me(technically you) hahahahah to funny. once i figure out the file not found im going to tryout your code gonna finish mine too. i was gonna get each line and seprate the different parts into a list and pick the items from the list.
@resserone13 sorry, didn't know 😢
Often, file not found errors are because the file is not in your current working directory.
Try adding up front:
import os print( os.path.abspath( os.getcwd() ) ) print (os.listdir() )
Alternatively, provide the full path to your file.
Another problem that happens commonly on posix systems like iOS/Unix, is that case matters. On windows it shouldn't matter.
@JonB i got it to find the file with the full path and now in using just the file name... i think i was missing a '' at the beginning of the file name now im getting
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape
with this code
with open('\US_state_populations2.csv', 'r') as f: pass
@resserone13 yes, it is the reason why my code reads it in binary (rb).
@resserone13 in python you need to use forward slash
/for file names. Backslash is interpreted as special characters. Hence \U is interpreted as a unicode character prefix.
@JonB well seen, I did not remark this \ in the file name. I had a problem of ucode in my .csv, reason for which I used rb and I thought it was the same problem, shame on me.
@JonB thanks for the heads up on the backslashes. i havent worked with file that much and i never noticed.. thanks the isssue was a typo new it too but i had copied and pasted then i typed it. that along with the backslash error it was all mixed up... thanks for all you advice as always.
this was what i turned in for my homework. my teach said there was some name errors and that the file didnt run completely. maybe he was talking about the type of file i uploaded. it worked fine in my comp. i was using spyder only cus im on my comp. here was the homework and my code
Create a single Python script (.py) that completes the following steps:
Read the "US_state_populations.csv" file into Python using any method you like.
Create a new file called "alphabetic.csv". This file should contain the same 3 columns (state names, 2019 populations, and 2010 populations) separated by commas, but they should sorted alphabetically by the state name. You may write to this file using any method you like. Hint: When you sort the state names, make sure the 2010 and 2019 populations are sorted with their respective states.
Calculate the change in population between 2010 and 2019 (change = pop2019 - pop2010). Create a new file called "popchange.txt". This file should contain 2 columns: state names and population change, separated by a blank space. The file should contain a header line. You should write to this file manually using a for loop (not numpy, pandas, or any other library).
Upload your Python script, "alphabetic.csv" and "popchange.txt" to this Canvas assignment.
list_of_list = [] #variable with file name. file_name = 'C:/Users/resse/Desktop/projects/poptest/US_state_populations2.csv' new_file = 'C:/Users/resse/Desktop/projects/poptest/abc2.csv' pop_diff = 'C:/Users/resse/Desktop/projects/poptest/pdiff.csv' #opens and closes file. with open(file_name, 'r') as f: #loops over file line by line. f.seek(23) for line in f: #saves each line as a list items = line.strip().split(',') #adds each list to a list list_of_list.append(items) #sorts the list s = sorted(list_of_list) #prints the sorted list print(s) #opens the new file and adds a sorted list to it. with open(new_file, 'w') as n_f: for i in s: n_f.write(f'\n {i[0]}, {i[1]}, {i[2]}') with open(pop_diff, 'w') as d_f: #writess the colum headers d_f.write(f'p19, p10, pdiff') #loops through a sorted list of states and population numbers for i in s: #calculates the population difference diff = int(i[1]) - int(i[2]) #writes each years population to a colum and #writes the population difference d_f.write(f'\n{i[1]}, {i[2]}, {diff}')
Obviously there are going to be file not found errors if you use an absolute path that doesnt exist on your professor's computer. You should use a relative path.
also, I ll point out that you didnt meet the file naming requirements-- you should be creating alphabetic.txt and popchange.txt...
@JonB your right. i submitted the wrong file types and the files were named incorrectly. ill have to pay more attention to things like that.. the were now working on a dice rolling exerecise and a mystery door game show ... ill make sure to submit the proper file types..
Why read .csv files without using ??
- resserone13
@ccc i depends on how you wanted to go about it. the teacher mention that some students used csv module. its a really basic class. i used what was explained in class.
- bigobeurre
This post is deleted!last edited by bigobeurre
- bigobeurre
This post is deleted!last edited by