Python File Upload rename, Zip and save - python

My requirement is
File upload,
Rename the file,
Compress it - make it as a zip,
Save in required location,
unzip the zip file,
Remove the zip file,
save the renamed file and filesize in DB
Renaming and zipping the file is not happening. Let me add my code here
#app.route('/temp/upload', methods=['POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
random_string = generate_random(5)
print "Random string=> ", random_string # wqzhp
print "f.filename->>> ", f.filename # form3.pdf
fn = f.filename
fname = (fn).split(".")
random_file_name = fname[0] + '-' + random_string + "." + fname[1]
print "random_file_name>> ", random_file_name # form3-wqzhp.pdf
actual_file = os.path.join(load_path, random_file_name)
print "Actual file -> ", actual_file # uploadedDocs/1/form3-wqzhp.pdf
zip_f_name = actual_file + ".zip"
print "zip_f_name--> ", zip_f_name # uploadedDocs/1/form3-wqzhp.pdf.zip
f.save(zip_f_name)
zip_ref = zipfile.ZipFile(zip_f_name, 'r')
zip_ref.extractall(load_path)
zip_ref.close()
os.remove(zip_f_name)
if update_file_status(random_file_name, 0, csvid) is False:
return "update failed"
return "success"
When the zip is extracted, only original file uploaded is unzipped. zipping with the renamed file is not happening! How to save the renamed file in zip?
Any Clues!

Related

How can I ensure a dataframe has completed being written via pandas.to_csv()?

I have been creating a little script that queries a database and returns the result. I have then been using Pandas.to_csv() to write it out to a CSV tempfile before I upload that CSV result to a cloud location. The trouble I am running into is ensuring that the pandas.to_csv() function has completed writing the CSV tempfile before I upload it to the cloud location. The only way I have consistently ensured that that date makes it to the temp file before the upload is by keeping the
print(temp.tell())
line of code in the example below. If I comment it out, no data gets uploaded.
Example code below:
def write_to_temporary_csv_file(df, file_name, token, folder_id):
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as temp:
print("DataFrame: ", df)
df.to_csv(temp, index=False, encoding='utf-8')
print("temp.tell() size: ", temp.tell())
print("File size: ", str(round((os.stat(temp.name).st_size/1024), 2)), "kb")
new_file_path = tempfile.gettempdir() + '/' + customer_name + '_' + file_name + '_' + current_date + '.csv'
## Check if newly created renamed temp file already exist, if it does remove it to create it
remove_temporary_file(new_file_path)
os.link(temp.name, new_file_path)
upload_response = upload_file(token, folder_id, new_file_path)
## Remove both the temp file and the newly created renamed temp file
remove_temporary_file(temp.name)
remove_temporary_file(new_file_path)
Image 1 (with temp.tell() included:
Image 2 (with temp.tell() commented out:
I think it might be caused by the fact that you keep your file opened (as long as you are inside the with block). That might cause the content not being flushed to disk.
def write_to_temporary_csv_file(df, file_name, token, folder_id):
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as temp:
print("DataFrame: ", df)
df.to_csv(temp, index=False, encoding='utf-8')
# at this point we can close the file by exiting the with block
print("temp.tell() size: ", temp.tell())
print("File size: ", str(round((os.stat(temp.name).st_size/1024), 2)), "kb")
new_file_path = tempfile.gettempdir() + '/' + customer_name + '_' + file_name + '_' + current_date + '.csv'
## Check if newly created renamed temp file already exist, if it does remove it to create it
remove_temporary_file(new_file_path)
os.link(temp.name, new_file_path)
upload_response = upload_file(token, folder_id, new_file_path)
## Remove both the temp file and the newly created renamed temp file
remove_temporary_file(temp.name)
remove_temporary_file(new_file_path)

How to get file metadata from external drive in Python?

I wrote a Python script that collects file metadata (filename, creation date, creation time, last modified data, last modified time) from a file directory. However, when the directory is a path that is located in an external hard drive the script doesn't work. I can't figure out why.
Here is the code:
import os
from os.path import basename
import datetime
import time
def getSize(filename):
st = os.stat(filename)
print st
return st.st_size
#get last modified date
def getMTime(filename):
fileModTime = os.path.getmtime(filename)
return fileModTime
#get creation date
def getCTime(filename):
fileModTime = os.path.getctime(filename)
return fileModTime
#get data from directory
MyDirectory = "H:\0_tempfiles\150115_Portfolio\Work\Work\BarBackUp"
MyExtension = ".jpg"
#write to file
WorkingDirectory = "C:\\Users\Admin\Downloads\demo\\"
MyTxtFile = WorkingDirectory + "fileData6.txt"
delim = ";"
with open(MyTxtFile, 'wb') as f:
f.write(delim.join(["FILENAME", "FILESIZE", "mDATE","mTIME",
"cDATE","cTIME"]) + "\n")
for root, dirs, files in os.walk(MyDirectory):
for file in files:
if file.endswith(MyExtension):
#get File Name
a = (os.path.join(root, file))
#print a
filename = a
MyFileName = basename(a)
#get File Size
MyFileSize = getSize(filename) / 1000
print MyFileName + " >>> file size: " + str(MyFileSize) + "Kb"
#get modification time V2
modTimeV2 = getMTime(filename)
modTimeV2 = time.strftime("%Y/%d/%m;%I:%M:%S %p", \
time.localtime(modTimeV2))
print "time modified: " + str(modTimeV2)
#get creation time
creTime = getCTime(filename)
creTime = time.strftime("%Y/%d/%m;%I:%M:%S %p", \
time.localtime(creTime))
print "time created: " + str(creTime)
#--------
#write data to file
entry = delim.join([str(MyFileName), str(MyFileSize), \
str(modTimeV2), str(creTime)]) + "\n"
f.write(entry)
print "<<<<<<everything went fine>>>>>>"
Your code works fine for me. Your "MyDirectory" variable has escape characters in it. Try adding an r in front of the quotations:
MyDirectory = r"H:\0_tempfiles\150115_Portfolio\Work\Work\BarBackUp"
or
MyDirectory = "H:/0_tempfiles/150115_Portfolio/Work/Work/BarBackUp"
or
MyDirectory = "H:\\0_tempfiles\\150115_Portfolio\\Work\\Work\\BarBackUp"

Avoid duplicate file names in a folder in Python

I want to download some files and save them in a folder and there may be some duplication in file names, so I want to avoid this to happen.
I think it needs an auto-naming system but now i don't know how to make it.
I used shutil and urllib2 to write my function.
This is a part of my code :
path = 'C:/DL/Others/'+filename+file_ext
with open(path, 'wb') as fp:
shutil.copyfileobj(req, fp)
As you know we can check that if a file exists or not by os.path.exists('path').
I wanna to rename my files and save them to avoid duplicated names using a pattern, for example by adding a number to file name.So if there was 4 files with same name, "fname", I want 4 files in this pattern :
fname - fname(1) - fname(2) - fname(3)
Something like this is probably reasonable:
path = 'c:/DL/Others/%s%s' % (filename, file_ext)
uniq = 1
while os.path.exists(path):
path = 'c:/DL/Others/%s_%d%s' % (filename, uniq, file_ext)
uniq += 1
If the original path doesn't exist you get no _1, but if it does exist it'll count up until it finds one that's free.
Track each filename's count as you create it:
fname_counts = {}
# ... whatever generates filename and file_ext goes here...
if filename + file_ext in fname_counts:
fname_counts[filename + file_ext] += 1
else:
fname_counts[filename + file_ext] = 0
# now check if it's a dupe when you create the path
if fname_counts[filename + file_ext]:
path = 'C:/DL/Others/%s_%s.%s' % (filename, fname_counts[filename + file_ext], file_ext)
else:
path = 'C:/DL/Others/' + filename + file_ext
Example at work with two duplicates ("test.txt"):
>>> filenames_and_exts = [('test', '.txt'), ('test', '.txt'), ('test2', '.txt'), ('test', '.cfg'), ('different_name', '.txt')]
>>> fname_counts = {}
>>> for filename, file_ext in filenames_and_exts:
if filename + file_ext in fname_counts:
fname_counts[filename + file_ext] += 1
else:
fname_counts[filename + file_ext] = 0
if fname_counts[filename + file_ext]:
path = 'C:/DL/Others/%s_%s%s' % (filename, fname_counts[filename + file_ext], file_ext)
else:
path = 'C:/DL/Others/' + filename + file_ext
print path
C:/DL/Others/test.txt
C:/DL/Others/test_1.txt
C:/DL/Others/test2.txt
C:/DL/Others/test.cfg
C:/DL/Others/different_name.txt

PySide QFTP put only uploading 35-40 bytes

When I use QFTP's put command to upload a file it only uploads around 40 bytes of the specified file. I'm catching the dataProgress signal and I'm getting the progress but the total size of the file is only read to be around 40 bytes. Is there anything wrong with my code, or is it a problem on the FTP server's side?
Here is my upload function:
def upload(self):
filename = QFileDialog.getOpenFileName(self, 'Upload File', '.')
fname = QIODevice(filename[0])
dataname = filename[0]
data = os.path.basename(dataname)
#data = data[data.find("/") + 1:]
print data
print fname
if not self.fileTree.currentItem():
self.qftp.put(fname, data)
elif "." in self.fileTree.currentItem().text(0):
self.qftp.put(fname, self.fileTree.currentItem().parent().text(0) + data)
elif self.fileTree.currentItem().text(0) == "/":
self.qftp.put(fname, data)
else:
return
Alright, figured out what I needed to do. I needed to create a QFile and read all of the bytes from that file and then pass that to the put command.
def upload(self):
filename = QFileDialog.getOpenFileName(self, 'Upload File', '.')
data = QFile(filename[0])
data.open(1)
qdata = QByteArray(data.readAll())
file = os.path.basename(filename[0])
print data
if not self.fileTree.currentItem():
self.qftp.put(qdata, file, self.qftp.TransferType())
elif "." in self.fileTree.currentItem().text(0):
self.qftp.put(qdata, self.fileTree.currentItem().parent().text(0) + file)
elif self.fileTree.currentItem().text(0) == "/":
self.qftp.put(qdata, file)
else:
return
I'm guessing that data = os.path.basename(dataname) means data is always a string containing the name of the file. Try changing this to be an open fileobj by using data = open(os.path.basename(dataname), 'rb')
edit
Looking at PySide.QtNetwork.QFtp.put(data, file[, type=Binary]) and PySide.QtNetwork.QFtp.put(dev, file[, type=Binary]) - the order of arguments is data/dev then file - so it's the wrong way around in your code...

Creating a zip file stream in python with Cherry Py?

I would like to create a zip file on the fly to serve to users through Cherry Py: I tried the following code that produced an invalid zip file:
#cherrypy.expose
def ZipDir(self, *args):
path = "\\".join(args)
output = StringIO.StringIO()
file = zipfile.ZipFile(output, "w")
zip_filename = path + ".zip"
cherrypy.response.headers['Content-Type'] = 'application/zip'
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="%s"' % (zip_filename,)
dir = filter(lambda x: x.lower().endswith(".mp3") or not ("." in x), os.listdir("G:\\Music\\" + path))
for f in dir:
file.write("G:\\Music\\" + path + "\\" + f,f,zipfile.ZIP_DEFLATED)
return output.getvalue()
The file size looks about right, but it doesnt register as a zip file with any acrhicing applications.
I forgot to call file.close() before the return. That fixed it!

Categories

Resources