Oct-12-2020, 04:33 AM
(Oct-12-2020, 04:21 AM)leodavinci1990 Wrote:(Oct-01-2020, 02:27 AM)bowlofred Wrote: Usually you'll want torstrip()
, which by default removes any whitespace from the right end.
result = [line.rstrip() for line in testfile if "2" in line]
Follow-up questions:
1)How do I, instead of having to give the full path of the file in the open function, just use test.txt ? In other words, how do I change the directory to the current directory where the Python file is currently being run from?
2) Why do I run into an error when opening the file using the following code:
f=open("C:\Users\Mohammad\Documents\test.txt", "r")
or
f=open("C:\Users\Mohammad\Documents\test.txt")
and why did putting an r before the filename in open(r"C:\Users\Mohammad\Documents\test.txt")
solve it?