I want to keep asking the user for a filename input until the filename that the user inputs does not exist anymore
Question:
I am trying to create program that creates a csv file in python wherein the user inputs the day of the class and the name of the class. If the input exists it would say file exists. If it doesn’t exist it will create the file and save it locally to where the python project folder is. I want it to keep repeating until the right input is given. I am fairly new to python.
this is my code
def csvcreate():
global class_day, class_name
class_day= input("What day of the class are you in?: ")
class_name = input("What is your subject name?: ")+class_day+'.csv'
if(os.path.exists(class_name) ==False):
f = open(class_name,'w+')
print("File created locally")
else:
print("Looks like it already exists")
#return back to asking for the class_day and class_name until it does not exist anymore
I tried while although it was still incorrect.
def csvcreate():
global class_day, class_name
class_day= input("What day of the class are you in?: ")
class_name = input("What is your subject name?: ")+class_day+'.csv'
while (os.path.exists(class_name)==False):
f = open(class_name,'w+')
print("File created")
while (os.path.exists(class_name)==True):
print("Already exists")
class_day= input("What day of the class are you in?: ")
class_name = input("What is your subject name?: ")+class_day+'.csv'
Answers:
It looks like you have confused the idea of a while
loop with an if
.
With your program you need a loop to make the program ask the user again. The if
is needed to decide whether to create the file or not.
Your first code uses the if
correctly, but doesn’t use a while
at all. I shall add a while
to the first code:
def csvcreate():
while True:
class_day = input("What day of the class are you in?: ")
subject_name = input("What is your subject name?: ")
class_name = subject_name + class_day + '.csv'
if os.path.exists(class_name):
print("Looks like it already exists")
else:
with open(class_name,'w+') as f:
print("File created locally")
I am trying to create program that creates a csv file in python wherein the user inputs the day of the class and the name of the class. If the input exists it would say file exists. If it doesn’t exist it will create the file and save it locally to where the python project folder is. I want it to keep repeating until the right input is given. I am fairly new to python.
this is my code
def csvcreate():
global class_day, class_name
class_day= input("What day of the class are you in?: ")
class_name = input("What is your subject name?: ")+class_day+'.csv'
if(os.path.exists(class_name) ==False):
f = open(class_name,'w+')
print("File created locally")
else:
print("Looks like it already exists")
#return back to asking for the class_day and class_name until it does not exist anymore
I tried while although it was still incorrect.
def csvcreate():
global class_day, class_name
class_day= input("What day of the class are you in?: ")
class_name = input("What is your subject name?: ")+class_day+'.csv'
while (os.path.exists(class_name)==False):
f = open(class_name,'w+')
print("File created")
while (os.path.exists(class_name)==True):
print("Already exists")
class_day= input("What day of the class are you in?: ")
class_name = input("What is your subject name?: ")+class_day+'.csv'
It looks like you have confused the idea of a while
loop with an if
.
With your program you need a loop to make the program ask the user again. The if
is needed to decide whether to create the file or not.
Your first code uses the if
correctly, but doesn’t use a while
at all. I shall add a while
to the first code:
def csvcreate():
while True:
class_day = input("What day of the class are you in?: ")
subject_name = input("What is your subject name?: ")
class_name = subject_name + class_day + '.csv'
if os.path.exists(class_name):
print("Looks like it already exists")
else:
with open(class_name,'w+') as f:
print("File created locally")