Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import os
'''This function creating a new text file (if not exist) and storing two variables, name and city, which can be used further in different functions
Otherwise, check if data in created file exist.'''
def setup_time():
exists = os.path.isfile("settings.txt") #Returning boolean (True) if file "settings.thx" exist.
if not exists: #Otherwise, a new file will be created in write mode. Inputs name and city will be saved and stored.
f = open("settings.txt", "w+")
print("")
name = input("What shall I call you: ")
city = input("Which city are you in: ")
f.write("name:" + name + "\n")
f.write("city:" + city + "\n")
f.write("state:complete")
f.close()
else: #This else statement will be run if a text file already exists and check its data.
settings_file = open("settings.txt")
for setting in settings_file:
if "state:complete" in setting: #While name and city already exist (state:complete) in "settings.txt"
settings_file.close() #Just close the file
break
'''The purpose of this function is to delete text file (data inside) after the main function will end,
so that a new data (name and city) can be created and stored next time while main code run.'''
def reset():
exists = os.path.isfile("settings.txt") #To avoid the error, checking if the file exists
if exists:
os.remove("settings.txt") #If True, then deleting the file
setup_time() #Calling function setup_time to create a new file and new data (name and city)
'''The "arg" is taken from the main code. It is either 0 or 1, depend on which argument from the file we need, either name or city.'''
def load_setting(arg):
settings_file = open("settings.txt")
if arg == 0:
for setting in settings_file:
if "name:" in setting:
name = setting.replace('name:','') #Taking just the user input name. Will be used while the program is running and printing username/calling the user.
settings_file.close()
return name
elif arg == 1: #Similar to above, but argument city which was defined by the user as an input on beginning can be used in function weather_ask.
for setting in settings_file:
if "city:" in setting:
city = setting.replace('city:','')
settings_file.close()
return city