This is the code i have used
file_name = username ".txt"
folder = "/Users/ymalek_/Desktop"
myfile = open(os.path.join(folder, file_name), "r")
try:
lines = myfile.readline()
GetTopic = print(lines[-1])
if GetTopic == "Language":
Language(topic="Media Language") #pre-defined function
if GetTopic == "Industries":
Language(topic="Media Industries") #pre-defined function
if GetTopic == "Representation":
Language(topic="Media Representation") #pre-defined function
if GetTopic == "Audience":
Language(topic="Media Audience") #pre-defined function
except:
print("Could not find what your last weakness was")
This code does not run and my program automatically skips to the code after this - the pre-defined functions aren't running
CodePudding user response:
I think you meant to read all the lines from the file and look at the last one. Like this:
try:
lines = myfile.readlines()
GetTopic = lines[-1].strip()
...
Your code read one line into lines
and lines[-1]
was a single character and GetTopic = print(lines[-1])
meant that GetTopic
was None
and so not equal to any of your literal strings.
CodePudding user response:
readline()
returns a string - the first line of the document.readlines()
returns an array, which is probably what you're expecting.- I prefer to use
lines = [x.strip() for x in myfile.readlines()]
- I prefer to use
- The result of assigning a
print()
is alwaysNone
. Python isn't the command line.