【新手】python copy 文件报错

chenyansong 2014-07-24 09:53:09
最近写了一个copy版本包的小程序,但是执行到copy的过程报错,没有拷贝下来。
服务器目录:主干目录://10.2.3.4/DDDD/RIS/2014-07下面很多版本文件,文件名称类似:类似ddddd-1.0.3.520140724230456.33569.exe。要取最新包到本地。

我自己写了一个,可以判断到2014-07文件夹下面,但是取不到最新的包。能否指点一二。?
新手,才学习Python。
...全文
571 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
前面 sourceDir =os.path.join(usefulpath, dir1); 后面 for i in range(len(sourceDir)): #遍历一个字符串内的字符? time_1 = os.path.getmtime(sourceDir[i]); time_2 = os.path.getmtime(sourceDir[i+1]); if (float(time_1) >= float(time_2)) : sourceFile = os.path.join(sourceDir[i],file) ;#这里不是很奇怪吗sourceDir 到底是什么? else : sourceFile = os.path.join(sourceDir[i+1],file) ;
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
请输入存放路径文件路径:d:\UIH Traceback (most recent call last): File "E:\Projects\fxdemo\UIH\01copy.py", line 53, in <module> sourceFile = os.path.join(sourceDir[i],file) ; File "E:\Python26\lib\ntpath.py", line 73, in join elif isabs(b): File "E:\Python26\lib\ntpath.py", line 57, in isabs s = splitdrive(s)[1] File "E:\Python26\lib\ntpath.py", line 125, in splitdrive if p[1:2] == ':': TypeError: 'type' object is unsubscriptable
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
import os import os.path import shutil import sys import time from shutil import * from glob import glob path = raw_input("请输入文件路径:") usefulpath = path.replace('\\', '/') if usefulpath.endswith("/"): usefulpath = usefulpath[:-1] if not os.path.exists(usefulpath): print "路径错误!" elif not os.path.isdir(usefulpath): print "输入的不是目录!" else: filelist = os.listdir(usefulpath) for i in range(len(filelist)): if 'Branches' == filelist[i]: del filelist[i]; dir1 = max(filelist); sourceDir =os.path.join(usefulpath, dir1); path1 = raw_input("请输入存放路径文件路径:"); targetFile = path.replace('\\', '/'); def copyFiles(sourceDir, targetDir): sourceFile = os.path.join(sourceDir, file) targetFile = os.path.join(targetDir, file) if os.path.isfile(sourceFile): if not os.path.exists(targetDir): os.makedirs(targetDir) if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))): open(targetFile, "wb").write(open(sourceFile, "rb").read()) if os.path.isdir(sourceFile): First_Directory = False shutil.copyfile(sourceFile, targetFile) for i in range(len(sourceDir)): time_1 = os.path.getmtime(sourceDir[i]); time_2 = os.path.getmtime(sourceDir[i+1]); if (float(time_1) >= float(time_2)) : sourceFile = os.path.join(sourceDir[i],file) ; else : sourceFile = os.path.join(sourceDir[i+1],file) ; print targetFile;
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
不是标点的问题,我早改过了。
  • 打赏
  • 举报
回复
LZ应学会看报错信息。。。
  • 打赏
  • 举报
回复
os.path,join
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
改成os.path,join(userfulpath, dir1)后报错: Traceback (most recent call last): File "E:\Projects\fxdemo\UIH\01copy.py", line 32, in <module> sourceDir =os.path,join(userfulpath, dir1); NameError: name 'join' is not defined
乔巴好萌 2014-07-25
  • 打赏
  • 举报
回复
1) sourceDir = usefulpath +"/" + dir1 +"/"; 这个使用方式不好 改成os.path,join(userfulpath, dir1) 因为"/"意味着windows平台不能用 2) 考虑用shutil 3) 递归是可以的 但是对目录本身是没法做拷贝的吧 应该加判断 if os.path.isdir(dir): continue
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
主要是有一个判断的过程,需要判断文件夹中的包文件是最新的,才拷贝到本地。
  • 打赏
  • 举报
回复
引用 6 楼 chenyansong 的回复:
Traceback (most recent call last): File "E:\Projects\fxdemo\UIH\01copy.py", line 51, in <module> sourceFile = os.path.join(sourceDir[i],file) ; File "E:\Python26\lib\ntpath.py", line 73, in join elif isabs(b): File "E:\Python26\lib\ntpath.py", line 57, in isabs s = splitdrive(s)[1] File "E:\Python26\lib\ntpath.py", line 125, in splitdrive if p[1:2] == ':': TypeError: 'type' object is unsubscriptable
你的变量p不是序列,无法进行下标操作
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
Traceback (most recent call last): File "E:\Projects\fxdemo\UIH\01copy.py", line 51, in <module> sourceFile = os.path.join(sourceDir[i],file) ; File "E:\Python26\lib\ntpath.py", line 73, in join elif isabs(b): File "E:\Python26\lib\ntpath.py", line 57, in isabs s = splitdrive(s)[1] File "E:\Python26\lib\ntpath.py", line 125, in splitdrive if p[1:2] == ':': TypeError: 'type' object is unsubscriptable
  • 打赏
  • 举报
回复
def copyFiles(sourceDir, targetDir): sourceFile = os.path.join(sourceDir, file) targetFile = os.path.join(targetDir, file) 这两个file哪里来的?
  • 打赏
  • 举报
回复
用递归的方式很容易出错的 报错信息是什么? 看看shutil,可能可以找到比较简便的批量拷贝操作方式
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
import os import os.path import shutil import sys import time path = raw_input("请输入文件路径:") usefulpath = path.replace('\\', '/') if usefulpath.endswith("/"): usefulpath = usefulpath[:-1] if not os.path.exists(usefulpath): print "路径错误!" elif not os.path.isdir(usefulpath): print "输入的不是目录!" else: filelist = os.listdir(usefulpath) for i in range(len(filelist)): if 'Branches' == filelist[i]: del filelist[i]; dir1 = max(filelist); sourceDir = usefulpath +"/" + dir1 +"/"; path1 = raw_input("请输入存放路径文件路径:"); targetFile = path.replace('\\', '/'); def copyFiles(sourceDir, targetDir): sourceFile = os.path.join(sourceDir, file) targetFile = os.path.join(targetDir, file) if os.path.isfile(sourceFile): if not os.path.exists(targetDir): os.makedirs(targetDir) if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))): open(targetFile, "wb").write(open(sourceFile, "rb").read()) if os.path.isdir(sourceFile): First_Directory = False copyFiles(sourceFile, targetFile) for i in range(len(sourceDir)): time_1 = os.path.getmtime(sourceDir[i]); time_2 = os.path.getmtime(sourceDir[i+1]); if (float(time_1) >= float(time_2)) : sourceFile = os.path.join(sourceDir[i],file) ; else : sourceFile = os.path.join(sourceDir[i+1],file) ; print targetFile;
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
如何通过'文件时间属性可获得'最新的文件?
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
2014-07 我是先删除Branches的属性,然后再使用max来筛选出来的。那个最新的文件是无法使用同样的方法来做的。
angel_su 2014-07-25
  • 打赏
  • 举报
回复
一样是路径带时间戳,感觉你能筛选出2014-07,类似的方法也能筛选出Daily-FULL-RIS.1.0.0.20140725.233666.exe...
woderwang 2014-07-25
  • 打赏
  • 举报
回复
首先要弄清楚最新文件意思,我的理解有2个。1最新更新的文件,根据文件时间属性可获得;2 根据文件名名字带version号获得,比如1.0,1.1 .. etc
chenyansong 2014-07-25
  • 打赏
  • 举报
回复
重新改写一遍,现在能把版本文件夹下所有版本全部拷贝下来,但是这个不是我需要,我只需要拷贝最新文件夹的最新版本。\\10.3.2.4\DAPENG_Publish\RIS\2014-07 这个文件夹下面最新的那个文件:Daily-FULL-RIS.1.0.0.20140725.233666.exe. 输入的路径是:\\10.3.2.4\DAPENG_Publish\RIS. 能否帮忙提供一下如果取最新的文件的思路,谢谢!
chenyansong 2014-07-24
  • 打赏
  • 举报
回复
我明天把代码发上来
加载更多回复(1)

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧