鱼C论坛

 找回密码
 立即注册
查看: 1116|回复: 1

[已解决]今天培训没有来,帮我一下python

[复制链接]
发表于 2018-3-1 19:15:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写一个程序文件的复制,要求能复制所有文件
程序可以手动输入源文件和目标文件名
(注释,只复制单个文件,不复制文件夹
最佳答案
2018-3-1 20:35:39
  1. import os

  2. def copyfile(source_fullpath,target_fullpath):
  3.     try:
  4.         with open(source_fullpath,"rb") as f:
  5.             content=f.read()
  6.         with open(target_fullpath, "wb") as f:
  7.             f.write(content)
  8.     except OSError as reason:
  9.         print(os.path.split(source_fullpath)[1],"复制失败")
  10.         print("原因:",str(reason))

  11. source_path = input("请输入源文件夹:")
  12. target_path = input("请输入目标文件夹:")

  13. os.chdir(source_path) #把源文件夹切换为当前工作目录
  14. for file in os.listdir(source_path): #列出文件夹下所有文件
  15.     if os.path.isfile(file): #判断是否是文件
  16.         source_fullpath = os.path.join(source_path,file) #os.path.join把路径和文件名组合成完整路径
  17.         target_fullpath = os.path.join(target_path,file)
  18.         copyfile(source_fullpath, target_fullpath)
  19.         print("正在复制",file)
复制代码

  1. import os
  2. import shutil

  3. source_path = input("请输入源文件夹:")
  4. target_path = input("请输入目标文件夹:")

  5. os.chdir(source_path) #把源文件夹切换为当前工作目录
  6. for file in os.listdir(source_path): #列出文件夹下所有文件
  7.     if os.path.isfile(file): #判断是否是文件
  8.         source_fullpath = os.path.join(source_path,file) #os.path.join把路径和文件名组合成完整路径
  9.         target_fullpath = os.path.join(target_path,file)
  10.         shutil.copyfile(source_fullpath, target_fullpath)
  11.         print("正在复制",file)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-3-1 20:35:39 | 显示全部楼层    本楼为最佳答案   
  1. import os

  2. def copyfile(source_fullpath,target_fullpath):
  3.     try:
  4.         with open(source_fullpath,"rb") as f:
  5.             content=f.read()
  6.         with open(target_fullpath, "wb") as f:
  7.             f.write(content)
  8.     except OSError as reason:
  9.         print(os.path.split(source_fullpath)[1],"复制失败")
  10.         print("原因:",str(reason))

  11. source_path = input("请输入源文件夹:")
  12. target_path = input("请输入目标文件夹:")

  13. os.chdir(source_path) #把源文件夹切换为当前工作目录
  14. for file in os.listdir(source_path): #列出文件夹下所有文件
  15.     if os.path.isfile(file): #判断是否是文件
  16.         source_fullpath = os.path.join(source_path,file) #os.path.join把路径和文件名组合成完整路径
  17.         target_fullpath = os.path.join(target_path,file)
  18.         copyfile(source_fullpath, target_fullpath)
  19.         print("正在复制",file)
复制代码

  1. import os
  2. import shutil

  3. source_path = input("请输入源文件夹:")
  4. target_path = input("请输入目标文件夹:")

  5. os.chdir(source_path) #把源文件夹切换为当前工作目录
  6. for file in os.listdir(source_path): #列出文件夹下所有文件
  7.     if os.path.isfile(file): #判断是否是文件
  8.         source_fullpath = os.path.join(source_path,file) #os.path.join把路径和文件名组合成完整路径
  9.         target_fullpath = os.path.join(target_path,file)
  10.         shutil.copyfile(source_fullpath, target_fullpath)
  11.         print("正在复制",file)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-8 02:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表