i am trying to copy a file from a local drive to a Network storage. The file is a TXT file with 6380 lines. Opening the Target file, the copy of the file is not complete, it stops at line 6285 in the middle of the line. The File Itself is a G-Code with 103 kb.
Source File Line 6285: "Z17.168 Y7.393" and the file continues...
Target File Line 6285: "Z17.168 Y7.3" after this its the end of the file.
I tired several differnt copy operations:
from shutil import copyfile
copyfile(WorkDir + FileName, CNCDir + FileName)
or
os.system("copy "+WorkDir + FileName +" " + CNCDir + FileName)
or also the DOS XCOPY command.
The weird thing is, that if i perform the copy command in a separate dos window with the same syntax it works perfect, just by executing it from the PYTHON script, it does not copy the complete file.
Any ideas?
Get a few "debugging" conformation using the following:
Option 1 (blunt reading, should print content to cmd console or editor stdout):
file = open(“testfile.text”, “r”)
print file.read()
Option 2 (reading + loading filecontent into memory, write content from memory to new file):
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
Option 3 (excludet character issue at line 6285 and show its a memory loading issue):
from itertools import islice
# Print the "6285th" line.
with open(“testfile.text”) as lines:
for line in islice(lines, 6284, 6285):
print line
Option 4 is found here.
and
Option 5 is:
import linecache
linecache.getline(“testfile.text”, 2685)
Using Option 1 on the target file gives me:
Z17.154 Y7.462
Z17.159 Y7.439
Z17.163 Y7.416
Z17.168 Y7.3
"done"
Press any key to continue . . .
Using Option 2 gives:
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>"
And a TXT File with the same content like the Target file:
Last 3 lines:
Z17.159 Y7.439
Z17.163 Y7.416
Z17.168 Y7.3
I tried option 4 -6 aswell but i dont see a purpose behind that. With Option 5 I had to change the line numbe to 6284 because list was out of range with that number.
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)
Out file from Command line:
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. . .
Related
I am testing below python code to export content from one txt file to another but in destination contents are getting copied with some different language (may be chinese) with improper
# - *- coding: utf- 8 - *-
from sys import argv
from os.path import exists
script,from_file,to_file = argv
print "Does the output file exist ? %r" %exists(to_file)
print "If 'YES' hit Enter to proceed or terminate by CTRL+C "
raw_input('?')
infile=open(from_file)
file1=infile.read()
outfile=open(to_file,'w')
file2=outfile.write(file1)
infile.close()
outfile.close()
try this code to copy 1 file to another :
with open("from_file") as f:
with open("to_file", "w") as f1:
for line in f:
f1.write(line)
I am getting a feedback error message that I can't seem to resolve. I have a csv file that I am trying to read and generate pdf files based on the county they fall in. If there is only one map in that county then I do not need to append the files (code TBD once this hurdle is resolved as I am sure I will run into the same issue with the code when using pyPDF2) and want to simply copy the map to a new directory with a new name. The shutil.copyfile does not seem to recognize the path as valid for County3 which meets the condition to execute this command.
Map.csv file
County Maps
County1 C:\maps\map1.pdf
County1 C:\maps\map2.pdf
County2 C:\maps\map1.pdf
County2 C:\maps\map3.pdf
County3 C:\maps\map3.pdf
County4 C:\maps\map2.pdf
County4 C:\maps\map3.pdf
County4 C:\maps\map4.pdf
My code:
import csv, os
import shutil
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
merged_file = PdfFileMerger()
counties = {}
with open(r'C:\maps\Maps.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for n, row in enumerate(reader):
if not n:
continue
county, location = row
if county not in counties:
counties[county] = list()
counties[county].append((location))
for k, v in counties.items():
newPdfFile = ('C:\maps\Maps\JoinedMaps\County-' + k +'.pdf')
if len(str(v).split(',')) > 1:
print newPdfFile
else:
shutil.copyfile(str(v),newPdfFile)
print 'v: ' + str(v)
Feedback message:
C:\maps\Maps\JoinedMaps\County-County4.pdf
C:\maps\Maps\JoinedMaps\County-County1.pdf
v: ['C:\\maps\\map3.pdf']
Traceback (most recent call last):
File "<module2>", line 22, in <module>
File "C:\Python27\ArcGIS10.5\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename: "['C:\\\\maps\\\\map3.pdf']"
There are no blank lines in the csv file. In the csv file I tried changing the back slashes to forward slashes, double slashes, etc. I still get the error message. Is it because data is returned in brackets? If so, how do I strip these?
You are actually trying to create the file ['C:\maps\map3.pdf'], you can tell this because the error messages shows the filename its trying to create:
IOError: [Errno 22] invalid mode ('rb') or filename: "['C:\\\\maps\\\\map3.pdf']"
This value comes from the fact that you are converting to string, the value of the dictionary key, which is a list here:
shutil.copyfile(str(v),newPdfFile)
What you need to do is check if the list has more than one member or not, then step through each member of the list (the v) and copy the file.
for k, v in counties.items():
newPdfFile = (r'C:\maps\Maps\JoinedMaps\County-' + k +'.pdf')
if len(v) > 1:
print newPdfFile
else:
for filename in v:
shutil.copyfile(filename, newPdfFile)
print('v: {}'.format(filename))
I have been trying to write onto a file while using python but for some reason it keeps writing onto my console and not my created file. Yes I know this question has been asked before and yes i have used the .close() command. Here is my block of code.
myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r')
with open ('C:/Users/12345/nanostring.txt','w') as output:
for line in myfile:
Templist= line.split()
print line
print Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12]
print output
myfile.close()
output.close()
This should be as simple as:
>>> with open('somefile.txt', 'a') as the_file:
... the_file.write('Hello\n')
From The Documentation:
Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.
In python 2.7,
You can use >> after the print and use as name
So here it is print>>output,line
myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r')
with open ('C:/Users/12345/nanostring.txt','w') as output:
for line in myfile:
Templist= line.split()
print>>output,line # Note the changes
print>>output,Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12] # Note the changes
Note: print directly prints in terminal and print>>as name, prints to file.
this is just part of the long python script. there is a file called aqfile and it has many parameters. I would like to extract what is next to "OWNER" and "NS".
Note:
OWNER = text
NS = numbers
i could extract what is next to OWNER, because they were just text and i could extract.
for line in aqfile.readlines():
if string.find(line,"OWNER")>0:
print line
m=re.search('<(.*)>',line)
owner=incorp(m.group(1))
break
but when i try to modify the script to extract the numbers
for line in aqfile.readlines():
if string.find(line,"NS")>0:
print line
m=re.search('<(.*)>',line)
ns=incorp(m.group(1))
break
it doesnt work any more.
Can anyone help me?
this is the whole script
#Make a CSV file of datasetnames. pulseprog and, if avaible, (part of) the title
#Note: the whole file tree is read into memory!!! Do not start too high in the tree!!!
import os
import os.path
import fnmatch
import re
import string
max=20000
outfiledesc=0
def incorp(c):
#Vervang " door """ ,CRLF door blankos
c=c.replace('"','"""')
c=c.replace("\r"," ")
c=c.replace("\n"," ")
return "\"%s\"" % (c)
def process(arg,root,files):
global max
global outfiledesc
#Get name,expno,procno from the root
if "proc" in files:
procno = incorp(os.path.basename(root))
oneup = os.path.dirname(root)
oneup = os.path.dirname(oneup)
aqdir=oneup
expno = incorp(os.path.basename(oneup))
oneup = os.path.dirname(oneup)
dsname = incorp(os.path.basename(oneup))
#Read the titlefile, if any
if (os.path.isfile(root + "/title")):
f=open(root+"/title","r")
title=incorp(f.read(max))
f.close()
else:
title=""
#Grab the pulse program name from the acqus parameter
aqfile=open(aqdir+"/acqus")
for line in aqfile.readlines():
if string.find(line,"PULPROG")>0:
print line
m=re.search('<(.*)>',line)
pulprog=incorp(m.group(1))
break
towrite= "%s;%s;%s;%s;%s\n" % (dsname,expno,procno,pulprog,title)
outfiledesc.write(towrite)
#Main program
dialogline1="Starting point of the search"
dialogline2="Maximum length of the title"
dialogline3="output CSV file"
def1="/opt/topspin3.2/data/nmrafd/nmr"
def2="20000"
def3="/home/nmrafd/filelist.csv"
result = INPUT_DIALOG("CSV file creator","Create a CSV list",[dialogline1,dialogline2,dialogline3],[def1,def2,def3])
start=result[0]
tlength=int(result[1])
outfile=result[2]
#Search for procs files. They should be in any dataset.
outfiledesc = open(outfile,"w")
print start
os.path.walk(start,process,"")
outfiledesc.close()
I have a .txt file with multiple lines (Different yet simular for each one) that I want to add a "*.tmp" at the end.
I'm trying to use python2.7 regex to do this.
Here is what I have for the python script:
import sys
import os
import re
import shutil
#Sets the buildpath variable to equal replace one "\" with two "\\" for python code to input/output correctly
buildpath = sys.argv[1]
buildpath = buildpath.replace('\\', '\\\\')
#Opens a tmp file with read/write/append permissions
tf = open('tmp', 'a+')
#Opens the selenium script for scheduling job executions
with open('dumplist.txt') as f:
#Sets line as a variable for every line in the selenium script
for line in f.readlines():
#Sets build as a variable that will replace the \\build\path string in the line of the selenium script
build = re.sub (r'\\\\''.*',''+buildpath+'',line)
#Overwrites the build path string from the handler to the tmp file with all lines included from the selenium script
tf.write(build)
#Saves both "tmp" file and "selenium.html" file by closing them
tf.close()
f.close()
#Copies what was re-written in the tmp file, and writes it over the selenium script
shutil.copy('tmp', 'dumplist.txt')
#Deletes the tmp file
os.remove('tmp')
#exits the script
exit()
Current File Before Replacing the Line:
\\server\dir1\dir2\dir3
DUMP3f2b.tmp
1 File(s) 1,034,010,207 bytes
\\server\dir1_1\dir2_1\dir3_1
DUMP3354.tmp
1 File(s) 939,451,120 bytes
\\server\dir1_2\dir2_2\dir3_2
Current file after replacing string:
\*.tmp
DUMP3f2b.tmp
1 File(s) 1,034,010,207 bytes
\*.tmp
DUMP3354.tmp
1 File(s) 939,451,120 bytes
\*.tmp
Desired file after replacing string:
\\server\dir1\dir2\dir3\*.tmp
DUMP3f2b.tmp
1 File(s) 1,034,010,207 bytes
\\server\dir1_1\dir2_1\dir3_1\*.tmp
DUMP3354.tmp
1 File(s) 939,451,120 bytes
\\server\dir1_2\dir2_2\dir3_2\*.tmp
If anyone could help me in solving this that would be great. Thanks :)
You should use capturing groups:
>>> import re
>>> s = "\\server\dir1\dir2\dir3"
>>> print re.sub(r'(\\.*)', r'\\\1\*.tmp', s)
\\server\dir1\dir2\dir3\*.tmp
Then, modify build = re.sub (r'\\\\''.*',''+buildpath+'',line) line this way:
build = re.sub (r'(\\.*)', r'\\\1%s' % buildpath, line)
Also, you shouldn't call readlines(), just iterate over f:
for line in f: