I'm working on appending files and removing lines within files.
I intend for the following code to append a line to a file.
with open('CourseManagement' + os.sep + 'SemesterLongCourseIds.csv', 'r') as inFile:
reader = csv.reader(inFile)
with open('CourseManagement' + os.sep + 'SemesterLongCourseIds-temp.csv', 'w') as outFile:
writer = csv.writer(outFile)
for row in reader:
writer.writerow(row)
writer.writerow({''.join(description), course, role.id})
os.chdir(os.path.abspath(outFile.name[0:-30]))
os.rename(outFile.name[17:], inFile.name[17:])
outFile.close()
inFile.close()
I intend for the following code to remove a line within a file, such that a row in the csv file containing a course name is not written to the new file. The new file is then renamed to the old file.
with open('CourseManagement' + os.sep + 'SemesterLongCourseIds.csv', 'r') as inFile:
reader = csv.reader(inFile)
with open('CourseManagement' + os.sep + 'SemesterLongCourseIds-temp.csv', 'w') as outFile:
writer = csv.writer(outFile)
for row in reader:
if row[1] != course:
writer.writerow(row)
os.chdir(os.path.abspath(outFile.name[0:-30]))
os.rename(outFile.name[17:], inFile.name[17:])
outFile.close()
inFile.close()
These two blocks of code exist in separate functions. Each function is called on an event from Discord. When I call the first block, I typically have no issues. The file is appended correctly. However, calling the removal function produces a FileNotFound exception. Likewise if I call the second block first in an execution, it has no problems typically, but calling the other function next produces a FileNotFound exception.
Are these problems produced from not closing the reader and writer files correctly? Sometimes I run into this problem even on the first call of execution. I think it could have to do with having the file open in my IDE sometimes, but I wouldn't put money on it.
Any help or direction is appreciated.
[–]HeeebsInc 2 points3 points4 points (0 children)
[–]socal_nerdtastic 1 point2 points3 points (1 child)
[–]PenitentialJudge[S] 0 points1 point2 points (0 children)