How to prompt for a filename and then open it ( windows 11 )
Question:
I want to read a file that is in a different path from my python file.
This is what i tried to do and I finish by getting syntax errors every time.
fname = input("Enter file name: ")
fh = open("C:UsersComputerDesktopAssignment 7.1%s" %fname)
This is what I tried to do.
Answers:
You are using the wrong slashes (i.e
instead of \
). Also, check if the path you are trying to reach exists (Computer being a subfolder of the Users folder doesn’t sound right).
In addition, if you are going with string concatenation, I’d recommend using python’s f-strings, like so:
fh = open(f"C:\Users\Computer\Desktop\Assignment 7.1\{fname})
However, to avoid issues as you just encountered, I would just use os.path.join:
import os
path = os.path.join("C:", "Users", "Computer", "Desktop", "Assignment 7.1", fname)
fh = open(path)
I’d also change the variable names to be separated by underscores.
Secondly, it is preferable to use a context manager (i.e the with
keyword). The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point:
import os
file_name = input("Enter file name: ")
path = os.path.join("C:", "Users", "Computer", "Desktop", "Assignment 7.1", fname)
with open(path) as file_handler:
file_content = file_handler.read() # to get the files content
You can also read more about how to handle reading and writing from files in python here.
To open the file you should use built-in open() function
The open() function returns a file object and you use a read() method for reading the content of the file:
fh = open("C:\UsersComputerDesktopAssignment 7.1", "r")
print(fh.read())
The ” character is Python strings is used to "escape", to type special characters like newlines or tabs. So, if you want to type backslash, you have to type it as \
, giving C:\Users\Computer\Desktop\Assignment 7.1\
.
In general, I’d advise using the pathlib
module to prevent these headaches. You can then use the forward slash, which does not have to be escaped.
from pathlib import Path
root_path = Path("C:") / "Users" / "Computer" / "Desktop" / "Assignment 7.1"
fname = input("Enter file name: ")
fh = open(root_path / fname)
I want to read a file that is in a different path from my python file.
This is what i tried to do and I finish by getting syntax errors every time.
fname = input("Enter file name: ")
fh = open("C:UsersComputerDesktopAssignment 7.1%s" %fname)
This is what I tried to do.
You are using the wrong slashes (i.e instead of
\
). Also, check if the path you are trying to reach exists (Computer being a subfolder of the Users folder doesn’t sound right).
In addition, if you are going with string concatenation, I’d recommend using python’s f-strings, like so:
fh = open(f"C:\Users\Computer\Desktop\Assignment 7.1\{fname})
However, to avoid issues as you just encountered, I would just use os.path.join:
import os
path = os.path.join("C:", "Users", "Computer", "Desktop", "Assignment 7.1", fname)
fh = open(path)
I’d also change the variable names to be separated by underscores.
Secondly, it is preferable to use a context manager (i.e the with
keyword). The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point:
import os
file_name = input("Enter file name: ")
path = os.path.join("C:", "Users", "Computer", "Desktop", "Assignment 7.1", fname)
with open(path) as file_handler:
file_content = file_handler.read() # to get the files content
You can also read more about how to handle reading and writing from files in python here.
To open the file you should use built-in open() function
The open() function returns a file object and you use a read() method for reading the content of the file:
fh = open("C:\UsersComputerDesktopAssignment 7.1", "r")
print(fh.read())
The ” character is Python strings is used to "escape", to type special characters like newlines or tabs. So, if you want to type backslash, you have to type it as \
, giving C:\Users\Computer\Desktop\Assignment 7.1\
.
In general, I’d advise using the pathlib
module to prevent these headaches. You can then use the forward slash, which does not have to be escaped.
from pathlib import Path
root_path = Path("C:") / "Users" / "Computer" / "Desktop" / "Assignment 7.1"
fname = input("Enter file name: ")
fh = open(root_path / fname)