1 Star 0 Fork 0

全宗/exercise-of-python3-2022

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
copy_file.py 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
全宗 提交于 2022-08-11 15:10 +08:00 . update copy_file.py.
'''
模拟linux的cp指令。
'''
from shutil import copy2
# copy2函数与copy函数的功能类似,但除了copy函数的功能外,还会复制文件的状态信息.
# 如最后访问时间,最后修改时间等。类似Linux的cp -p命令
# copy()文件复制。shutil可以简单地理解为sh + util,shell工具.
# shutil模块是对os模块的补充,主要针对文件的拷贝、删除、移动、压缩和解压操作。
# 测试文件是否存在
def test_file_exist(location):
try:
with open(location, mode="rb") as fileread:
fileread.read(8) # 检测能否读到数据
except IOError:
return 0
source = input("Source Full Path:")
location = source
# 检测来源文件是否存在。
if test_file_exist(location) == 0:
print("File is not found or don't have permission to access this file.")
exit()
destination = input("Destination Full Path:")
location = destination
# 检测目标文件是否存在。
if test_file_exist(location) != 0:
print("File exist.Can't overwrite.Try again,plz.")
exit()
copy2(source, destination)
print("Fin.")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/quan-zong/exercise-of-python3-2022.git
git@gitee.com:quan-zong/exercise-of-python3-2022.git
quan-zong
exercise-of-python3-2022
exercise-of-python3-2022
master

搜索帮助