我正在尝试将文件从本地驱动器复制到网络存储。该文件是一个6380行的TXT文件。打开目标文件时,文件的副本未完成,它在行中间的第6285行停止。该文件本身是一个103kb的G-Code。
源文件第6285行:"Z17.168 Y7.393“,文件继续...
目标文件第6285行:"Z17.168 Y7.3“之后是文件的结尾。
我尝试了几个不同的复制操作:
from shutil import copyfile
copyfile(WorkDir + FileName, CNCDir + FileName)或
os.system("copy "+WorkDir + FileName +" " + CNCDir + FileName)或者也可以使用DOS XCOPY命令。奇怪的是,如果我在一个单独的dos窗口中使用相同的语法执行copy命令,它将完美地工作,仅仅通过从PYTHON脚本执行它,它不会复制完整的文件。
有什么想法吗?
发布于 2018-02-18 18:14:48
使用以下命令获取一些“调试”确认:
选项1(生硬阅读,应将内容打印到cmd控制台或编辑器stdout):
file = open(“testfile.text”, “r”)
print file.read() 选项2(读取+加载文件内容到内存,将内容从内存写入新文件):
file = open(“testfile.text”, “r”)
count_lines = 0
counted_lines = 6380
content = []
for line in file:
count_line += 1
content.append(line)
print 'row : "%s" content : "%s"' % (count_lines, file)
if counted_lines == count_lines:
dest_file = open(“new_testfile.txt”,”w”)
for item in content:
dest_file.write(item)
dest_file.close()
else:
print 'error at line : %s' % count_lines选项3(排除第6285行的字符问题并显示其为内存加载问题):
from itertools import islice
# Print the "6285th" line.
with open(“testfile.text”) as lines:
for line in islice(lines, 6284, 6285):
print line选项4是found here。
和
选项5是:
import linecache
linecache.getline(“testfile.text”, 2685)发布于 2018-02-18 18:40:44
在目标文件上使用选项1可以得到以下结果:
Z17.154 Y7.462
Z17.159 Y7.439
Z17.163 Y7.416
Z17.168 Y7.3
"done"
Press any key to continue . . .使用选项2可以提供:
row : "6284" content : "<open file 'Z:\\10-Trash\\APR_Custom.SPF', mode 'r' at 0x0000000004800AE0>"
row : "6285" content : "<open file 'Z:\\10-Trash\\APR_Custom.SPF', mode 'r' at 0x0000000004800AE0>"和一个与Target文件内容相同的TXT文件:
最后3行:
Z17.159 Y7.439
Z17.163 Y7.416
Z17.168 Y7.3我也尝试了选项4 -6,但我看不到它背后的目的。对于选项5,我必须将行号更改为6284,因为列表超出了该编号的范围。
from itertools import islice
# Print the "6285th" line.
with open(CNCDir + FileName) as lines:
for line in islice(lines, 6284, 6285):
print "Option 3:"
print line
f=open(CNCDir + FileName)
lines=f.readlines()
print "Option 4:"
print lines[6284]
import linecache
print "Option 5:"
print linecache.getline(CNCDir + FileName, 6285)从命令行输出文件:
Copy to CNC:
Source: C:\F-Engrave\WorkDir\APR_Custom.SPF
Traget: Z:\10-Trash\APR_Custom.SPF
Option 3:
Z17.168 Y7.3
Option 4:
Z17.168 Y7.3
Option 5:
Z17.168 Y7.3
"done"
Press any key to contiune. . .https://stackoverflow.com/questions/48850358
复制相似问题