Python,exists判断文件是否存在

若如初见. 提交于 2020-01-18 22:26:12

命令 exists:
文件名字符串作为参数,如果文件存在的话,它将返回 True,否则将返回 False。

# -- coding: utf-8 --

from sys import argv      # 导入函数或者模块
from os.path import exists

(script, from_file, to_file) = argv   # 解包

print("Copying from %r to %r" % (from_file, to_file))

inputfile = open(from_file)
indata = inputfile.read()

print("The input file is %d bytes long" % len(indata))
print("the file content: %r" % indata)

print("Does the output file exist? %r" % exists(to_file))  # 判断文件存在

print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()

outputfile = open(to_file, 'w')  # 以写的方式打开文件
outputfile.write(indata)         # 写入数据

print("Alright, all done.")

outputfile.close()  # 关闭打开的文件
inputfile.close()

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!