如何用python对文本进行操作

本文通过多次尝试,详细记录了使用Python进行文件读取的过程,包括解决路径错误、语法错误等常见问题,最终成功读取并打印文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作为一个小白,这段内容学的很崩溃,不在于有多难,而是自己老是出错,记录一下吧

 with open('test_word') as if:
  File "<stdin>", line 1
    with open('test_word') as if:
                               ^
SyntaxError: invalid syntax
>>> with open('E:\lenovo\test_word','r') as f:
...     print(f.read())
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument: 'E:\\lenovo\test_word'
>>> with open('/lenovo/test_eord','r') as f:
...     print(f.read())
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/lenovo/test_eord'
>>> with open('/lenovo/test_word','r') as f:
...     print(f.read())
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/lenovo/test_word'
>>> with open('E:/lenovo/test_word','r') as f:
...     print(f.read())
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'E:/lenovo/test_word'
>>> with open('E:\lenovo\test_word.txt','r') as f:
...     print(f.read())
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument: 'E:\\lenovo\test_word.txt'
>>> with open('E:/lenovo/test_word','r') as f:
...     print(f.read())
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'E:/lenovo/test_word'
>>> with open('E:/lenovo/test_word.txt','r') as f:
...     print(f.read())
...
hello,world
写了将近10遍,才终于写通。大概真的是比较笨
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值