Python中文
首页
教程
问答
标签
搜索
登录
注册
在python中,程序将一个文件的内容复制到另一个文件的输出错误
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>即使文件存在,我也得到了错误的输出。下面是代码</p> <pre><code>from sys import argv from os.path import exists import sys import os script,fromf,tof=argv inf=open(fromf) if exists(str(inf))==True: indata=inf.read() outf=open(tof,'w') if exists(str(outf))==True: print("Error! Output file exists.") sys.exit() else: outf.write(indata) print("The task is accomplished.") else: print("Error! Input file doesn't exists.") </code></pre> <p>我传递的论点如下</p> <pre><code>python3 file.py aaa.txt bbb.txt </code></pre> <p>文件aaa.txt存在。。。但它仍然显示“错误!“输入文件不存在”</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p><code>os.path.exists</code>需要路径(字符串),而不是<code>file</code>对象</p> <p>您应该使用<code>fromf</code>作为参数:</p> <pre><code>if exists(fromf): # no need for " == True" # ... </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何合并多个PDF文件?
3 回答
如何合并多个xarray数据变量及其坐标?
2 回答
如何合并多个列中具有重复值的行
7 回答
如何合并多个唯一id
4 回答
如何合并多个图纸并使用图纸名称的名称重命名列名?
6 回答
如何合并多个字典并添加同一个键的值?(Python)
1 回答
如何合并多个搜索结果文件(pkl)以将它们全部打印在一起?
8 回答
如何合并多个数据帧
4 回答
如何合并多个数据帧并使用Pandas为假人添加列?
7 回答
如何合并多个数据帧并按时间戳排序
5 回答
如何合并多个数据帧的列表并用另一个lis标记每列
1 回答
如何合并多个数据框中的列
8 回答
如何合并多个文件?
5 回答
如何合并多个查询集?
2 回答
如何合并多个绘图?
5 回答
如何合并多个词典
9 回答
如何合并多个输入数据集(数据帧)?
6 回答
如何合并多条记录中拆分的文本行
3 回答
如何合并多索引列datafram
9 回答
如何合并多级(即多索引)数据帧?
1 回答