filename = 'x.txt'
with open(filename, 'r') as file_object:
contents = file_object.readlines()
print(contents)
for content in contents:
content = content.strip('\n')
print(content)
if len(content)==32:
contents.remove('content')
print(contents)
具体错误代码:
[PowerShell] 纯文本查看复制代码
PS C:\Users\ZhiHu\Documents\python_application> c:; cd 'c:\Users\ZhiHu\Documents\python_application'; & 'C:\Users\ZhiHu\AppData\Local\Programs\Python\Python310\python.exe' 'c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '55704' '--' 'c:\Users\ZhiHu\Documents\python_application\txt_delete.py'
['shfsdk2434jfhsdf\n', 'E389EBF0A6AA1CD9C9F976EEB4350F20\n', '23423\n', 'BF9477540C8AA9C6B3131B2723B3C17B']
shfsdk2434jfhsdf
E389EBF0A6AA1CD9C9F976EEB4350F20
Traceback (most recent call last):
File "C:\Users\ZhiHu\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\ZhiHu\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
cli.main()
File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file runpy.run_path(target, run_name="__main__")
File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "c:\Users\ZhiHu\Documents\python_application\txt_delete.py", line 11, in <module>
contents.remove('content')
ValueError: list.remove(x): x not in list
['shfsdk2434jfhsdf\n', 'E389EBF0A6AA1CD9C9F976EEB4350F20\n', '23423\n', 'BF9477540C8AA9C6B3131B2723B3C17B']
shfsdk2434jfhsdf
E389EBF0A6AA1CD9C9F976EEB4350F20
Traceback (most recent call last):
File "C:\Users\ZhiHu\Documents\python_application\txt_delete.py", line 11, in <module>
contents.remove(content)
ValueError: list.remove(x): x not in list
in_file = 'x.txt'
out_file = 'out.txt'
with open(out_file, 'w') as fo:
with open(in_file, 'r') as fi:
for line in fi:
if len(line.strip())==32:
fo.write(line)