please guys am a beginner in python and was trying to create a simple to-do-list program, but am stuck right now at the point of making directory. I will be posting the code below all suggest to help and make it better is welcome.
#!/usr/bin/env python3
def_directory = "~/Documents/"
def fi_le():
x = int(input("Enter The Length of Your To-Do-List: "))
#this next line of codes should generate the number of list you want
xf = list(range(1, (x + 1)))
#This next liine asks for the To-do list name
import os.path
directory = def_directory
namer = input("Enter File Name: ")
file_name = namer + ".txt"
file_path = os.path.join(directory, file_name)
if not os.path.isdir(directory):
os.mkdir(directory)
f = open(file_path, "w+")
#This next line of code takes in user To-Do-List
for i in xf:
l = ("=>" + str(i) + ". ")
f.close()
def dir_check():
print("\n")
print("*NOTE: PLEASE NOTE YOUR DEFAULT DIR FOR SAVED FILE IS THE 'DOCUMENTS FOLDER'")
print("IF YOU WANT TO CHANGE DIR ENTER 'C' TO PROCEED ELSE ENTER 'D' TO USE DEFAULT FOLDER")
print("\n")
ask_user = input("Do you want to Proceed with DEFAULT FOLDER: ").upper()
if ask_user == "D":
fi_le()
elif ask_user == "C":
user_folder_name = ("~/" + input("Specify Folder Name: "))
def_directory = user_folder_name
fi_le()
else:
print("Please Enter 'D' Or 'C' to Continue")
dir_check()
dir_check()
print("\n" * 5)
print("===============> End <===============")
There are multiple issues
Indentation is important
You need to define directory variable as a full path:
For instance: in Unix system:
def_directory = "/Users/PycharmProjects/StackOverFlow-pip/"
You are actually not writing to the file. To write you need to call write function.
for i in xf:
l = ("=>" + str(i) + ". ")
f.write(l)
Full corrected Code:
def_directory = "/Users/PycharmProjects/StackOverFlow-pip/"
def fi_le():
x = int(input("Enter The Length of Your To-Do-List: "))
#this next line of codes should generate the number of list you want
xf = list(range(1, (x + 1)))
#This next liine asks for the To-do list name
import os.path
directory = def_directory
namer = input("Enter File Name: ")
file_name = namer + ".txt"
file_path = os.path.join(directory, file_name)
if not os.path.isdir(directory):
os.mkdir(directory)
f = open(file_path, "w+")
#This next line of code takes in user To-Do-List
for i in xf:
u = input("Enter the value")
f.write(("=>" + str(i) + ". " + u))
f.close()
def dir_check():
print("\n")
print("*NOTE: PLEASE NOTE YOUR DEFAULT DIR FOR SAVED FILE IS THE 'DOCUMENTS FOLDER'")
print("IF YOU WANT TO CHANGE DIR ENTER 'C' TO PROCEED ELSE ENTER 'D' TO USE DEFAULT FOLDER")
print("\n")
ask_user = input("Do you want to Proceed with DEFAULT FOLDER: ").upper()
if ask_user == "D":
fi_le()
elif ask_user == "C":
user_folder_name = ("~/" + input("Specify Folder Name: "))
def_directory = user_folder_name
fi_le()
else:
print("Please Enter 'D' Or 'C' to Continue")
dir_check()
dir_check()
print("\n" * 5)
print("===============> End <===============")
Output:
=>1. Eat Hamburger=>2. Eat Cheeseburger
Possible issue is that the intermediate directory does not exist. If that is the case, use os.makedirs instead of os.mkdir.
See documentation here: https://docs.python.org/3/library/os.html#os.makedirs
Additionally of Ahmet, you should not do this kind of check before creating a directory on the file system:
if not os.path.isdir(directory):
os.mkdir(directory)
instead, you should try to create it directly, and manage the exception:
try:
os.mkdirs(directory)
except FileExistsError:
# do something
Related
I have this code where the user has to input the name of a file which includes a message and the name of a file where the message must be written after its encrypted via Caesar-Cipher.
I would like to validate the inputs, so that if there's a wrong input, the code won't crash but ask the user for a valid file path until the user inputs it.
I have some familiarity with validation using the while loop, however I couldn't apply it here without ruining other parts of the code.
Any suggestions are appreciated.
def open_file(source_path: str, dest_path: str):
with open(source_path, mode='r') as fd:
while line := fd.readline():
return line
def write_to_file(dest_path: str, line: str):
with open(dest_path, mode='a') as fd:
fd.write(line)
source_path = input("Enter the name of the file including the message: ")
dest_path = input("Enter the name of the file where the encrypted message will be written: ")
MODE_ENCRYPT = 1
def caesar(source: str, dest: str, steps, mode):
alphabet = "abcdefghijklmnopqrstuvwxyzabcABCDEFGHIJKLMNOPQRSTUVWXYZABC"
alpha_len: int = len(alphabet)
new_data = ""
file = open_file(source_path, dest_path)
for char in file:
index = alphabet.find(char)
if index == -1:
new_data += char
else:
# compute first parth
changed = index + steps if mode == MODE_ENCRYPT else index - steps
# make an offset
changed %= alpha_len
new_data += alphabet[changed:changed + 1]
write_to_file(dest_path, new_data)
return new_data
while True:
# Validating input key
key = input("Enter the key: ")
try:
key = int(key)
except ValueError:
print("Please enter a valid key: ")
continue
break
ciphered = caesar(source_path, dest_path, key, MODE_ENCRYPT)
Not quite sure what you meant by not being able to use a while loop, but here is a simple way of checking if the paths exists using pathlib.
from pathlib import Path
while True:
source_path = Path(input("Enter the name of the file including the message: "))
if source_path.exists():
break
print("Please input a valid path")
while True:
dest_path = Path(input("Enter the name of the file where the encrypted message will be written: "))
if dest_path.exists():
break
print("Please input a valid path")
You can use inbuilt OS module in Python. Here is the code until the path is a valid path.
Note: Keeping a check for MAXIMUM RETRIES will help the code to not stuck in an infinite loop for the user.
import os
def getPath():
MAXIMUM_RETRIES = 5
count = 0
file_path = ""
while True:
count += 1
file_path = input("Enter the path: ")
if os.path.exists(file_path):
break
if count >= MAXIMUM_RETRIES:
print("You have reached maximum number or re-tries. Exiting...")
exit(1)
print("Invalid Path. Try again.")
return file_path
I'm a self taught programmer and im trying to make a ticketing system in Python where it accepts multiple inputs and reads from the file depending on the number of tickets. However, the previous inputs get overwritten by the newer inputs and I can't seem to fix it.
The output I get is like this:
J
a
k
e
25
M
a
l
e
But I'd like for the output to look like this:
Jake;25;Male
I've attached the code of this program below. Any help would be greatly appreciated. Thank you.
import sys, select, os
from os import system
def option_1():
with open(input("Input file name with extension: "), 'w+') as f:
people = int(input("\nHow many tickets: "))
name_l = []
age_l = []
sex_l = []
for p in range(people):
name = str(input("\nName: "))
name_l.append(name)
age = int(input("\nAge: "))
age_l.append(age)
sex = str(input("\nGender: "))
sex_l.append(sex)
f.flush()
for item in name:
f.write("%s\n" %item)
for item in [age]:
f.write("%s\n" %item)
for item in sex:
f.write("%s\n" %item)
x=0
print("\nTotal Ticket: ", people, '\n')
for p in range(1, people + 1):
print("Ticket No: ", p)
print("Name: ", name)
print("Age: ", age)
print("Sex: ", sex)
x += 1
def option_2():
with open(input('Input file name with extension: '), 'r') as f:
fileDir = os.path.dirname(os.path.realpath('__file__'))
f.flush()
f_contents = f.read()
print("\n")
print(f_contents, end = '')
def main():
system('cls')
print("\nTicket Booking System\n")
print("\n1. Ticket Reservation")
print("\n2. Read")
print("\n0. Exit Menu")
print('\n')
while True:
option = int(input("Choose an option: "))
if option < 0 or option > 2:
print("Please choose a number according to the menu!")
else:
while True:
if option == 1:
system('cls')
option_1()
user_input=input("Press ENTER to return to main menu: \n")
if((not user_input) or (int(user_input)<=0)):
main()
elif option == 2:
system('cls')
option_2()
user_input=input("Press ENTER to return to main menu: \n")
if((not user_input) or (int(user_input)<=0)):
main()
else:
exit()
if __name__ == "__main__":
main()
If you have a recent version of python you can use an f-string to compose the format you require.
You need a loop to iterate over the information you have collected.
You may just need this:
...
f.flush()
for name,age,sex in zip(name_l, age_l, sex_l):
f.write(f"{name};{age};{sex}\n")
...
Also, the printout to the console needs a similar loop:
print("\nTotal Ticket: ", people, '\n')
for p,(name,age,sex) in enumerate(zip(name_l, age_l, sex_l), start = 1):
print("Ticket No: ", p)
print("Name: ", name)
print("Age: ", age)
print("Sex: ", sex)
I have created a program where I have two text files: "places.txt" and "verbs.txt" and It asks the user to choose between these two files. After they've chosen it quizzes the user on the English translation from the Spanish word and returns the correct answer once the user has completed their "test". However the program runs smoothly if the text files are free in the folder for python I have created on my Mac but once I put these files and the .py file in a subfolder it says files can't be found. I want to share this .py file along with the text files but would there be a way I can fix this error?
def CreateQuiz(i):
# here i'm creating the keys and values of the "flashcards"
f = open(fileList[i],'r') # using the read function for both files
EngSpanVocab= {} # this converts the lists in the text files to dictionaries
for line in f:
#here this trims the empty lines in the text files
line = line.strip().split(':')
engWord = line[0]
spanWord = line[1].split(',')
EngSpanVocab[engWord] = spanWord
placeList = list(EngSpanVocab.keys())
while True:
num = input('How many words in your quiz? ==>')
try:
num = int(num)
if num <= 0 or num >= 10:
print('Number must be greater than zero and less than or equal to 10')
else:
correct = 0
#this takes the user input
for j in range(num):
val = random.choice(placeList)
spa = input('Enter a valid spanish phrase for '+val+'\n'+'==> ')
if spa in EngSpanVocab[val]:
correct = correct+1
if len(EngSpanVocab[val]) == 1:
#if answers are correct the program returns value
print('Correct. Good work\n')
else:
data = EngSpanVocab[val].copy()
data.remove(spa)
print('Correct. You could also have chosen',*data,'\n')
else:
print('Incorrect, right answer was',*EngSpanVocab[val])
#gives back the user answer as a percentage right out of a 100%
prob = round((correct/num)*100,2)
print('\nYou got '+str(correct)+' out of '+str(num)+', which is '+str(prob)+'%'+'\n')
break
except:
print('You must enter an integer')
def write(wrongDict, targetFile):
# Open
writeFile = open(targetFile, 'w')
# Write entry
for key in wrongDict.keys():
## Key
writeFile.write(key)
writeFile.write(':')
## Value(s)
for value in wrongDict[key]:
# If key has multiple values or user chooses more than 1 word to be quizzed on
if value == wrongDict[key][(len(wrongDict[key])) - 1]:
writeFile.write(value)
else:
writeFile.write('%s,'%value)
writeFile.write('\n')
# Close
writeFile.close()
print ('Incorrect answers written to',targetFile,'.')
def writewrong(wringDict):
#this is for the file that will be written in
string_1= input("Filename (defaults to \'wrong.txt\'):")
if string_1== ' ':
target_file='wrong.txt'
else:
target_file= string_1
# this checs if it already exists and if it does then it overwrites what was on it previously
if os.path.isfile(target)==True:
while True:
string_2=input("File already exists. Overwrite? (Yes or No):")
if string_2== ' ':
write(wrongDict, target_file)
break
else:
over_list=[]
for i in string_1:
if i.isalpha(): ovrList.append(i)
ovr = ''.join(ovrList)
ovr = ovr.lower()
if ovr.isalpha() == True:
#### Evaluate answer
if ovr[0] == 'y':
write(wrongDict, target)
break
elif ovr[0] == 'n':
break
else:
print ('Invalid input.\n')
### If not, create
else:
write(wrongDict, target)
def MainMenu():
## # this is just the standad menu when you first run the program
if len(fileList) == 0:
print('Error! No file found')
else:
print( "Vocabulary Program:\nChoose a file with the proper number or press Q to quit" )
print(str(1) ,"Places.txt")
print(str(2) ,"Verbs.txt")
while True:
#this takes the user input given and opens up the right text file depending on what the user wants
MainMenu()
userChoice = input('==> ')
if userChoice == '1':
data = open("places.txt",'r')
CreateQuiz(0)
elif userChoice == '2':
data = open("verbs.txt",'r')
CreateQuiz(1)
elif userChoice == 'Q':
break
else:
print('Choose a Valid Option!!\n')
break
You are probably not running the script from within the new folder, so it tries to load the files from the directory from where you run the script.
Try setting the directory:
import os
directory = os.path.dirname(os.path.abspath(__file__))
data = open(directory + "/places.txt",'r')
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I am making a Python program where I can work with files from any part of my computer. It's not done yet but I ran into a problem. This is my code:
import os
from os.path import join
import subprocess
def opening_file(lookfor):
global store_1
for root, dirs, files in os.walk('/home/'):
if lookfor in files:
file = join(root, lookfor)
store_1.append(join(root, lookfor))
if len(store_1) <= 0:
ask_1 = str(input("Your file was not found. Do you want to try again(Y/n)?:"))
#This is where I have the problem
elif len(store_1) > 0:
print("Your file was found")
subprocess.call(["xdg-open", file])
#print(store_1)
store_1 = []
print("Welcome to our program for working with files.")
print("Press O for opening and editing a file, C for making a copy of a file. M for moving a file and R for renaming a file. If you are done working with the file, press F to end the program.")
choice = str(input("Your choice:"))
if choice == "O" or choice == "o":
lookfor = input("File name(make sure to include the extension as well):")
opening_file(lookfor)
I want to know how can i go back to the if statement the user entered with his/her input when the file is not being found.
Is there any way I can do this? I have googled but I cannot find a solution to my problem. My OS is Ubuntu 16.04.
Simply use while?
import os
from os.path import join
import subprocess
def opening_file(lookfor):
global store_1
for root, dirs, files in os.walk('/home/'):
if lookfor in files:
file = join(root, lookfor)
store_1.append(join(root, lookfor))
if len(store_1) <= 0:
ask_1 = str(input("Your file was not found. Do you want to try again(Y/n)?:"))
#This is where I have the problem
elif len(store_1) > 0:
print("Your file was found")
subprocess.call(["xdg-open", file])
#print(store_1)
store_1 = []
print("Welcome to our program for working with files.")
choice = ""
while(choice.lower() != "f"):
print("Press O for opening and editing a file, C for making a copy of a file. M for moving a file and R for renaming a file. If you are done working with the file, press F to end the program.")
choice = str(input("Your choice:"))
if choice == "O" or choice == "o":
lookfor = input("File name(make sure to include the extension as well):")
opening_file(lookfor)
import os
from os.path import join
import subprocess
def opening_file(lookfor):
store_1 = []
for root, dirs, files in os.walk('/home/'):
if lookfor in files:
file = join(root, lookfor)
store_1.append(join(root, lookfor))
if len(store_1) <= 0:
ask_1 = str(input("Your file was not found. Do you want to try again(Y/n)?:"))
#This is where I have the problem
if (ask_1.lower() == 'y':
return 'y'
else:
return 'n'
else:
print("Your file was found")
subprocess.call(["xdg-open", file])
return store_1
#print(store_1)
def show_menu():
choice = str(input("Your choice:"))
if choice == "O" or choice == "o":
lookfor = input("File name(make sure to include the extension as well):")
result = opening_file(lookfor)
if result == 'y':
show_menu()
elif result == 'n':
#your code for no
else:
# you can manage the result list 'store_1'
print(result)
print("Welcome to our program for working with files.")
print("Press O for opening and editing a file, C for making a copy of a file.
M for moving a file and R for renaming a file. If you are done working with the file, press F to end the program.")
show_menu()
I am fairly new to python and I need to make a program to ask 10 questions, save the score into a file and allow someone to read the scores in from the file.
My problem: I need to check if the person who has done the quiz already has a record in the file, and if so, I need to add their score to the end of their record.
The records should look like this:
name,score,score,score,score,
etc so they can be split using commas.
I am also looking for the simplest answer, not the most efficient. Also, if you could comment the code, it would make it much easier. Here is my code so far:
import random
import math
import operator as op
import sys
import re
def test():
num1 = random.randint(1, 10)
num2 = random.randint(1, num1)
ops = {
'+': op.add,
'-': op.sub,
'*': op.mul,
}
keys = list(ops.keys())
rand_key = random.choice(keys)
operation = ops[rand_key]
correct_result = operation(num1, num2)
print ("What is {} {} {}?".format(num1, rand_key, num2))
while True:
try:
user_answer = int(input("Your answer: "))
except ValueError:
print("Only enter numbers!")
continue
else:
break
if user_answer != correct_result:
print ("Incorrect. The right answer is {}".format(correct_result))
return False
else:
print("Correct!")
return True
print("1. Are you a student?")
print("2. Are you a teacher?")
print("3. Exit")
while True:
try:
status = int(input("Please select an option:"))
except ValueError:
print("Please enter a number!")
else:
if status not in {1,2,3}:
print("Please enter a number in {1,2,3}!")
else:
break
if status == 1:
username=input("What is your name?")
while not re.match("^[A-Za-z ]*$", username) or username=="":
username=input(str("Please enter a valid name (it must not contain numbers or symbols)."))
print ("Hi {}! Wellcome to the Arithmetic quiz...".format(username))
while True:
try:
users_class = int(input("Which class are you in? (1,2 or 3)"))
except ValueError:
print("Please enter a number!")
else:
if users_class not in {1,2,3}:
print("Please enter a number in {1,2,3}!")
else:
break
correct_answers = 0
num_questions = 10
for i in range(num_questions):
if test():
correct_answers +=1
print("{}: You got {}/{} {} correct.".format(username, correct_answers, num_questions,
'question' if (correct_answers==1) else 'questions'))
if users_class == 1:
class1 = open("Class1.txt", "a+")
newRecord = username+ "," + str(correct_answers) + "," + "\n"
class1.write(newRecord)
class1.close()
elif users_class == 2:
class2 = open("Class2.txt", "a+")
newRecord = username+ "," + str(correct_answers) + "," + "\n"
class2.write(newRecord)
class2.close()
elif users_class == 3:
class3 = open("Class3.txt", "a+")
newRecord = username+ "," + str(correct_answers) + "," + "\n"
class3.write(newRecord)
class3.close()
else:
print("Sorry, we can not save your data as the class you entered is not valid.")
EDIT:
Add this function before your "test" function:
def writeUserScore(file, name, score):
with open (file, "r") as myfile:
s = myfile.read()
rows = s.split("\n")
data = {}
for row in rows:
tmp = row.split(",")
if len(tmp) >= 2: data[tmp[0]] = tmp[1:]
if name not in data:
data[name] = []
data[name].append(str(score))
output = ""
for name in data:
output = output + name + "," + ",".join(data[name]) + "\n"
handle = open(file, "w+")
handle.write(output)
handle.close()
After that, where you have "if users_class == 1:" do this:
writeUserScore("Class1.txt", username, str(correct_answers))
Do the same for the other two else ifs.
Let me know what you think!
Try using a dictionary to hold the existing file data.
Read the file in a variable called "str" for example. And then do something like this:
rows = str.split("\n")
data1 = {}
for row in rows:
tmp = row.split(",")
data1[tmp[0]] = tmp[1:]
When you have a new score you should then do:
if username not in data1:
data1[username] = []
data1[username] = str(correct_answers)
And to save the data back to the file:
output = ""
for name in data1:
output = outupt + name + "," + ",".join(data1[name]) | "\n"
And save the contents of "output" to the file.
PS: If you are not bound by the file format you can use a JSON file. I can tell you more about this if you wish.
Hope that helps,
Alex
First, define these functions:
from collections import defaultdict
def read_scores(users_class):
"""
If the score file for users_class does not exist, return an empty
defaultdict(list). If the score file does exist, read it in and return
it as a defaultdict(list). The keys of the dict are the user names,
and the values are lists of ints (the scores for each user)
"""
assert 0 <= users_class <= 3
result = defaultdict(list)
try:
lines =open("Class%d.txt"%users_class,'r').readlines()
except IOError:
return result
for line in lines:
# this line requires python3
user, *scores = line.strip().split(',')
# if you need to use python2, replace the above line
# with these two lines:
# line = line.strip().split(',')
# user, scores = line[0], line[1:]
result[user] = [int(s) for s in scores]
return result
def write_scores(users_class, all_scores):
"""
Write user scores to the appropriate file.
users_class is the class number, all scores is a dict kind of dict
returned by read_scores.
"""
f = open("Class%d.txt"%users_class,'w')
for user, scores in all_scores.items():
f.write("%s,%s\n"%(user, ','.join([str(s) for s in scores])))
def update_user_score(users_class, user_name, new_score):
"""
Update the appropriate score file for users_class.
Append new_score to user_name's existing scores. If the user has
no scores, a new record is created for them.
"""
scores = read_scores(users_class)
scores[user_name].append(new_score)
write_scores(users_class, scores)
Now, in the last portion of your code (where you actually write the scores out) becomes much simpler. Here's an example of writing some scores:
update_user_score(1, 'phil', 7)
update_user_score(1, 'phil', 6)
update_user_score(1, 'alice', 6)
update_user_score(1, 'phil', 9)
there will be two lines in Class1.txt:
phil,7,6,9
alice,6
We read the whole file into a dict (actually a defaultdict(list)),
and overwrite that same file with an updated dict. By using defaultdict(list), we don't have to worry about distinguishing between updating and adding a record.
Note also that we don't need separate if/elif cases to read/write the files. "Scores%d.txt"%users_class gives us the name of the file.