I try to read my input file.
Myfile = list(input("Please enter a filename with full dir: "))
fp = open(Myfile)
sstr = fp.read()
but always show the error:
"D:\Program Files\Python\python.exe" D:/Onebox/Python/my_help_doc/guo.py
Please enter a filename with full dir: 'E:\个人\郭周诺\Python\guo-python.txt'
Traceback (most recent call last):
File "D:/Onebox/Python/my_help_doc/guo.py", line 7, in <module>
fp = open(Myfile)
TypeError: expected str, bytes or os.PathLike object, not list
Process finished with exit code 1
you should have a path-like object not a list as param for the open
built-in function
from the docs :
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.
file is a path-like object
giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.)
you may use a string as a param for the open
function:
my_file = input("Please enter a filename with full dir: ")
fp = open(my_file)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.