I get this error when I run my code and reach the csv file part: line 96, in fieldnames self._fieldnames = next(self.reader) io.UnsupportedOperation: read
My code:
import csv
username = ""
global enteruser
global username
def user():
age = input("What is your age:")
year = input("What is your year group?")
name = input("What is your name?")
username = name+age+year
return username
def username_validator():
with open('usernames.csv', 'ab') as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
if enteruser or username == row["usernames"]: # if the username shall be on column 3 (-> index 2)
print ("is in file")
with open("user1.csv","ab") as quiz:
quizreader = csv.DictReader(quiz,delimiter=",")
for row in quizreader:
print(row["name"])
else:
print("doesnt work")
isuser = input("Do you have a username?")
if isuser == ("yes" or "Yes"):
enteruser = input("Enter username:")
username_validator()
elif isuser == ("no" or "No"):
user()
else:
None
print(username)
Not sure if this is what is causing your problem, but make sure to be careful with your use of global variables here. As your code stands now, it will always print an empty string at the last line, since you never update username
globally. As a rule of thumb, try to avoid using global variables when possible, and instead pass those variables as arguments to the functions.
Unless you explicitly tell your functions to use global variables, it will default to creating local variables. So here:
def user():
age = input("What is your age:")
year = input("What is your year group?")
name = input("What is your name?")
username = name+age+year
return username
We are actually not doing anything to the global variable username
, but rather simply creating a new local variable within the function which is returned. To update the global variable, either define it as global within the function (shown below):
def user():
global username
age = input("What is your age:")
year = input("What is your year group?")
name = input("What is your name?")
username = name+age+year
Alternatively, leave the function unchanged, and simply update the variable when you call the user()
function, as follows:
elif isuser == ("no" or "No"):
username=user()
else:
None
print(username)
EDIT: Take a close look at your if statements, read the link posted in the comments under OP.
Your error is caused by your file opening / closing.
You're trying to read from a file not opened for it.
From the python built in functions open function open has a declaration like:
open(name[, mode[, buffering]])
Here the arguments you can use are r: reading, w: writing, a: appending r+: read/write, w+: read/write, and a+: append/write for the mode. To these a binary tag can be added to the end like r+b
For your code you'll want to change your open lines from
with open("user1.csv","ab") as quiz:
to
with open('user1.csv', 'a+b') as quiz:
So really all you need is that + to allow append and read
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.