create file after positive exception FileNotFoundError - python

I have written script that parses a web page and saves data of interest in a CSV file. Before I open the data and use it in a second script I check if the file with data exist and if not I am running the parser script first. The odd behaviour of the second script is, that it is able to detect that there is no file, then the file is created, but when it is read for the first time it is empty (part of else statement). I tried to provide some delay by using the time.sleep() method, but it does not work. The explorer clearly shows that the file is not empty, but at the first run, script recognizes the file as empty. At the subsequent runs the scripts clearly sees the file and is able to properly recognize it content.
Maybe You have some explanation for this behaviour.
def open_file():
# TARGET_DIR and URL are global variables.
all_lines = []
try:
current_file = codecs.open(TARGET_DIR, 'r', 'utf-8')
except FileNotFoundError:
procesed_data = parse_site(URL)
save_parsed(procesed_data)
compare_parsed()
open_file()
else:
time.sleep(10)
data = csv.reader(current_file, delimiter=';')
for row in data:
all_lines.append(row)
current_file.close()
return all_lines

You got some recursion going on.
Another way to do it—assuming I understand correctly—is this:
import os
def open_file():
# TARGET_DIR and URL are global variables.
all_lines = []
# If the file is not there, make it.
if not os.path.isfile(TARGET_DIR):
procesed_data = parse_site(URL)
save_parsed(procesed_data)
compare_parsed()
# Here I am assuming the file has been created.
current_file = codecs.open(TARGET_DIR, 'r', 'utf-8')
data = csv.reader(current_file, delimiter=';')
for row in data:
all_lines.append(row)
current_file.close()
return all_lines

you should return the result of your internal open_file call, or just opening the file in your except block:
def open_file():
# TARGET_DIR and URL are hopefully constants
try:
current_file = codecs.open(TARGET_DIR, 'r', 'utf-8')
except FileNotFoundError:
procesed_data = parse_site(URL)
save_parsed(procesed_data)
compare_parsed()
current_file = codecs.open(TARGET_DIR, 'r', 'utf-8')
data = csv.reader(current_file, delimiter=';')
all_lines = list(data)
current_file.close()
return all_lines

Related

How to find first and last characters in a file using python?

I am stuck on this revision exercise which asks to copy an input file to an output file and return the first and last letters.
def copy_file(filename):
input_file = open(filename, "r")
content = input_file.read()
content[0]
content[1]
return content[0] + content[-1]
input_file.close()
Why do I get an error message which I try get the first and last letters? And how would I copy the file to the output file?
Here is the test:
input_f = "FreeAdvice.txt"
first_last_chars = copy_file(input_f)
print(first_last_chars)
print_content('cure737.txt')
Error Message:
FileNotFoundError: [Errno 2] No such file or directory: 'hjac737(my username).txt'
All the code after a return statement is never executed, a proper code editor would highlight it to you, so I recommend you use one. So the file was never closed. A good practice is to use a context manager for that : it will automatically call close for you, even in case of an exception, when you exit the scope (indentation level).
The code you provided also miss to write the file content, which may be causing the error you reported.
I explicitely used the "rt" (and "wt") mode for the files (althought they are defaults), because we want the first and last character of the file, so it supports Unicode (any character, not just ASCII).
def copy_file(filename):
with open(filename, "rt") as input_file:
content = input_file.read()
print(input_file.closed) # True
my_username = "LENORMJU"
output_file_name = my_username + ".txt"
with open(output_file_name, "wt") as output_file:
output_file.write(content)
print(output_file.closed) # True
# last: return the result
return content[0] + content[-1]
print(copy_file("so67730842.py"))
When I run this script (on itself), the file is copied and I get the output d) which is correct.

How to write rows in CSV file DYNAMICALLY in python?

I want to create a csv file and write data to it dynamically my script have to keep running 24/7 and csv files have to be created and written every 24 hours, right now all files are created when the program ends.
with open(file_name, 'r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
now = datetime.datetime.now()
dtString = now.strftime('%H:%M:%S')
writer = csv.writer(f)
writer.writerow(name, dtString)
Thanks in advance
Remove the file context. Use the earlier way of writing file. And keep doing flush() and fsync() on the file like shown below. That ensures that data is written to the file on disk.
f = open(FILENAME, MODE)
f.write(data)
f.write(data)
f.flush() #important part
os.fsync(f) # important part
For more info: see this link

AttributeError: 'NoneType' object has no attribute 'read'?

I am currently working on a few problems and I have been stuck on this one for quite some time. I cannot figure out how to get past the AttributeError.
Within the function that is causing the problem, I have another function called safeOpen, which opens files, but won't give an error if there isn't a file with that specific name.
def safeOpen(fileName):
try:
infile = open(fileName)
print('Opened')
except:
print('None')
In my main function, avgSpd, I have the user input the file name and it gets opened with safeOpen. However, when the program goes to read the file it gives me the following error:
AttributeError: 'NoneType' object has no attribute 'read'.
I have tested safeOpen to see if that is the problem, but testing safeOpen alone shows that safeOpen is working. Any help would be appreciated.
def avgSpd():
fileName = input('Enter the file name: ')
infile = safeOpen(fileName)
content = infile.read()
Your safeOpen doesn't have a return statement, so the infile variable in the avgSpd function is set to None.
try:
def safeOpen(fileName):
try:
infile = open(fileName)
print('Opened')
return infile
except:
print('None')
return None
It's worth noting that it's best to use context managers (that is, the `with' statement) to make sure that the file is safely opened. You could also check using the os.path.exists() function if your file exists rather than using try/except, too.
Something like:
import os
def safeOpen(fileName):
if os.path.exists(fileName):
with open(fileName) as f:
content = f.read()
else:
content = None
return content
def avgSpd():
fileName = input('Enter the file name: ')
content = safeOpen(fileName)
if content is not None:
do_more_stuff()
you did not returned the infile variable in inner function. corrected version will be:
def safeOpen(fileName):
try:
infile = open(fileName)
print('Opened')
except:
print('None')
return infile
def avgSpd():
fileName = input('Enter the file name: ')
infile = safeOpen(fileName)
content = infile.read()
return content

Accessing files in my program folder in a more pythonic way

My program uses a csv file to load up some initialization info.
I have a config file that loads data from this csv file. I am building a web app as part of this program, and the config file is accessed from various points in the entire application.
This program must operate cross-platform.
Problem: Depending on who calls the config file and where the caller lives in the file tree, the csv operation is throwing IOError errors. The csv data isn't even accessed, but on import of the config file, the csv read portion executes anyway.
The code below is rife with Band-Aids...
# print os.getcwd()
try:
with open('value_addresses.csv') as file: # located in code folder. used extensively below
reader = csv.reader(file)
lines = [l for l in reader]
except IOError:
try:
with open('_code/value_addresses.csv') as file: #
reader = csv.reader(file)
lines = [l for l in reader]
except IOError:
with open('../_code/value_addresses.csv') as file: #
reader = csv.reader(file)
lines = [l for l in reader]
I would refactor the common code into a function.
def read_file(path):
with open(path) as file:
reader = csv.reader(file)
lines = [l for l in reader]
return lines
try:
read_file("value_addresses.csv")
except IOError:
try:
read_file('_code/value_addresses.csv')
except IOError:
read_file('../_code/value_addresses.csv')
You can further simplify this by recursively figuring out the path to value_addresses.csv.
This is how I would do it:
from os.path import join
import csv
def myopen( filename, possible_dirs, mode ):
for dir in possible_dirs:
try:
return open(join(dir,filename),mode)
except IOError:
pass
raise IOError('File not found.')
with myopen('value_addresses.csv',['.','_code','../_code'],'r') as file:
reader = csv.reader(file)
lines = [l for l in reader]
Although maybe you want to look more into the specific IOError you are getting, but either way this is the general approach I would take.

No-such-file-or-directory error in Python

I have written this code
import os
import csv
import time
class upload_CSV:
def __init__(self):
coup = []
self.coup = coup
self.csv_name = 'Coup.csv'
def loader(self,rw):
with open(self.csv_name,'rb') as csv_file:
reader = csv.reader(csv_file,delimiter=',')
for row in reader:
self.coup.append(row[rw])
self.coup = self.coup[1:]
csv_file.flush()
csv_file.close()
return self.coup
def update(self,rw,message):
#try:
with open(self.csv_name,'rb') as csv_file1:
reader = csv.reader(csv_file1,delimiter=',')
csv_file1.flush()#To clean the register for reuse
csv_file1.close()
#except Exception as ex:
#error = 'An error occured loading data in the reader'
# #raise
# return ex
os.remove(self.csv_name)
writer = csv.writer(open(self.csv_name,'wb'))
for row in reader:
if row[rw]==message:
print str(row),' has been removed'
else:
writer.writerow(row)
return message
I am trying to read the content of a csv to a list first. Once i get the relevant data, i need to go back to my csv and create a new entry without that record. I keep getting the single error
Line 27 in update
with open(csv_name,'rb')as csvfile1:
Python: IOError: [Errno 2] No such file or directory 'Coup.csv'
when i call the Update function
I have looked at this question Python: IOError: [Errno 2] No such file or directory but nothing seems to work. Its as if the first function has a lock on the file. Any help would be appreciated
It would be enormously helpful if we saw the traceback to know exactly what line is producing the error...but here is a start...
First, you have two spots in your code where you are working with a filename that expects to only be available in the current directory. That is one possible point of failure in your code if you run it outside the directory containing the file:
self.csv_name = 'Coup.csv'
...
with open(self.csv_name,'rb') as csv_file:
...
with open('Coup.csv~','rb') as csv_file1:
...
And then, you are also referring to a variable that won't exist:
def update(self,rw,message):
...
# self.csv_name? or csv_file1?
os.remove(csv_name)
writer = csv.writer(open(csv_name,'wb'))
Also, how can you be sure this temp file will exist? Is it guaranteed? I normally wouldn't recommend relying on a system-temporary file.
with open('Coup.csv~','rb') as csv_file1:

Categories

Resources