简体   繁体   English

读取 python 中的文件 function

[英]Reading File function in python

hello I am trying to apply the file reading function in python您好我正在尝试在 python 中应用读取 function 的文件

f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
print(f.read())

It keeps giving me the error of它一直给我的错误

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\hamza\\Desktop\\family.txt'

please help me to resolve this issue请帮我解决这个问题

Thankyou谢谢

That is the correct code to read a file.这是读取文件的正确代码。 C:\\Users\\hamza\\Desktop\\family.txt" must not exist. C:\\Users\\hamza\\Desktop\\family.txt"不得存在。

  1. You can try following code also,您也可以尝试以下代码,

     path="C:\\Users\\hamza\\Desktop\\family.txt" f = open(path,"r") print(f.read())

    But your code is also correct.但是您的代码也是正确的。

  2. Please check if file is present on desktop and name of file is correct or not请检查桌面上是否存在文件以及文件名是否正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:argparse读取csv文件以起作用 - Python: argparse reading csv file to function 将另一个文件中的函数读入python中的按钮 - reading function from another file into a button in python Python 读取行 function 未读取文件中的第一行 - Python readlines function not reading first line in file 使用Python自动执行文件读取和功能应用 - Automating the file reading and function applying using Python 使用自定义函数将单元测试读取到python中的文件 - Reading a Unit Test to a file in python with a custom function python [regex] - 搜索 function 在从文件中读取行时不起作用 - python [regex] - search function is not working while reading lines from a file 为什么这个 python 函数不能正确读取我的 json 文件字符串? - Why is this python function not reading my json File string properly? 通过将文件作为参数传递给python脚本中的函数来写入和读取新文件 - Writing and reading a new file by passing the files as an argument to a function in a python script 从 Python Azure 函数读取 Zip 文件的内容 - Reading a Zip File's Content from a Python Azure Function 在 Python 中从文件中读取二进制数据并将其传递给 C++ 函数 - Reading binary data from file in Python and passing it to a C++ function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM