Reputation: 641
I am using Python 3.4
This is my code:
fileObject = open("names.txt", "r")
counter = 0
for line in fileObject:
fileObject.readline()
counter = counter + 1
print("The number of lines is: ", counter)
When I run this, no matter where I place the file I repeatedly receive the following Traceback error:
FileNotFoundError: [Errno 2] No such file or directory: 'names.txt'
The only possibly relevant info that I found on this in my book (I am currently taking a Python class) is the following:
If open function receives a filename that does not contain a path, assumes that file is in same directory as program.
But it doesn't matter where I put the file, this won't work. Although, when I "prefix the path literal with the letter r", such as the following:
fileObject = open(r"C:\Python34\names.txt", "r")
counter = 0
for line in fileObject:
fileObject.readline()
counter = counter + 1
print("The number of lines is: ", counter)
Then, it will work, but not display the lines that contain a digit.(How can I make it include these?) Lastly, I also had to alter my code to allow a user to enter a file that they wanted to read, but only read the first 5 lines of the file, if less than will display all lines. It follows(This goes along with me not being able to simply type the name("names.txt") to search for the file):
fileName = input("Enter the location of the file: ")
fileObject = open("r" + "\"" + fileName + "\"", "r")
for counter in range(5):
while fileObject != "":
file = fileObject.readline()
print(file)
fileObject.close()
It gives me the following Traceback error:
OSError: [Errno 22] Invalid argument: 'r"C:\\Python34\\names.txt"'
Why am I receiving this? (I also noticed that it adds an extra "\" next to each one that is already present - The error message contains 4 "\" while I only typed 2.)
In summary:
(1.) "FileNotFoundError" for "names.txt".
(2.) How to make the "readline()"
methd recognize digits(int's/float's).
(3.) "Invalid Argument"
Traceback when a user inputs a file to be searched for.
(4.) Why this last question [the error message] displays double the "\\"
characters than I actually entered.
Upvotes: 0
Views: 3208
Reputation: 2057
When using open
, and a relative path was supplied(like "cat.txt"
), Python reads from the current working directory, which is where you started the interpreter in the terminal. Make sure you are in the correct path.
To parse string into numbers, either use int()
or float()
, float()
it's safer as int("1.1")
will throw an error
The r prefix only works when you are supplying a string literal, for example r"123"
will work. Doing "r" + string
will simply add the character r to the beginning of the string. More on why r
makes opening the file work later. you don't need to add quotes around the string, as that will make the path invalid. You only see quotes around the strings in the terminal because Python wants to indicate to you that the string is a string.
\\
is displayed instead of \
since \
is a special control character that is used for escape sequences, such as \n
and \t
, to get just a back slash character \\
is used. This is also why opening the files work when you use the r
flag, which treats the string as is
Upvotes: 1