I'm trying to copy files from source to destination. The file is there in the source location. The exception is raised on the destination file. Specifically, the line try: os.close(fout)
.The traceback.print_exc()
is UnboundLocalError: local variable 'fout' referenced before assignment
fout
is assigned prior to that line, fout = os.open(dst, WRITE_FLAGS, stat.st_mode).
It appears to only throw an exception on certain files. I've been away from Python for a little while now. I do not really understand why the exception is being thrown. I believe fout
is not opening or creating the file. But why is there no exception when fout = os.open (dst, WRITE_FLAGS, stat.st_mode)
or when writing os.write(fout, x)
? Any help to point me in the right direction is greatly appreciated.
Original copy file code is from this thread How to Copy Files Fast.
import os
import traceback
class CTError(Exception):
def __init__(self, errors):
self.errors = errors
try:
O_BINARY = os.O_BINARY
except:
O_BINARY = 0
READ_FLAGS = os.O_RDONLY | O_BINARY
WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY
BUFFER_SIZE = 128*1024
def copyfile(src, dst):
try:
fin = os.open(src, READ_FLAGS)
stat = os.fstat(fin)
fout = os.open(dst, WRITE_FLAGS, stat.st_mode)
for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""):
os.write(fout, x)
finally:
try: os.close(fin)
except:
#fin2 = open("C:\\Users\\testUser\\ScriptErrLogs\\saveErrors.txt", 'a+')
#fin2.write(traceback.print_exc())
#fin2.close()
pass
try: os.close(fout)
except:
fin2 = open("C:\\Users\\testUser\\ScriptErrLogs\\saveErrors.txt", 'a+')
e = str(traceback.print_exc())
fin2.write(e)
fin2.close()
print os.path.isfile(src)
print os.path.isfile(dst)
pass
DEBUG = 1
f = open('C:\\Users\\testUser\\Scripts\\Python\\testImport.csv','r')
lines = f.readlines()
for line in lines:
temp = line.split('\\')
tempFileName = line[0:-1]
tempPath = 'C:\\Users\\testUser\\testSaveLocation\\testSave'
# DEBUG prints
if DEBUG == 0:
print '#### LINE ####'
print line
print '#### FILENAME ####'
print tempFileName
# Discard the first 3 elements to create the new location path
for entry in temp[3:]:
tempPath = tempPath + '\\'+ entry
# DEBUG prints
if DEBUG == 0:
print '#### TEMPPATH ####'
print tempPath
# Check path exists
pathExists = os.path.exists(tempPath)
# Create directory structure
if not pathExists and not '.pdf' in entry:
try:
os.mkdir(tempPath)
except OSError as error:
print error
# Copy file to new location
if '.pdf' in entry:
try:
copyfile(line[:-1],tempPath[:-1])
except OSError as error:
print error