So I ran into an issue.. I'm attempting to open a text file and read it line by line either normally or in reverse based on a variable's value. Python keeps throwing a AttributeError: __enter__
error; but I was mostly just trying to see if this is even possible.
Example code:
def function(rev):
# - open file in reverse format - open file normally
with reversed(list(open("test.txt"))) if rev == True else open("test.txt") as dict:
for line in dict:
print (line)
pass
pass
pass
function(True)
Result:
...
with reversed(list(open("test.txt"))) if rev == True else open("test.txt") as dict:
AttributeError: __enter__
How can I do this without having to create a standard if statement for both possibilities & 2 different with-as loops for the same procedure?