Hello I was wondering if there is a way of reading a unformatted file which is stored in gzformat that is stored in zipformat. also this should be done without extracting any file format and outside pluggin than what python 2.7 can provides.
I just want to read entire or line (depending on minimal for memory usage) of the content if there is a bunch of gz in a zipfile then store it in a txt format outside.
My code as of now can read out .log, .gz, .zip also .zip in a zip.
the idea is that it should repeat itself until we get the main file !
Thanks in advance :=)
.zip--->.gz--->unformattedfile
def decompress_file(filepath,type,file=None):
result = {}
if type.lower() == '.log':
if file ==None:
inFile = open(filepath, 'r')
return getLog(inFile)
else:
return getLog(file)
if type.lower() == '.gz':
if file == None:
inFile = gzip.open(filepath, "r")
else:
#content = io.BytesIO(file.read())
with file:
Gz_file= open(file,'r')
for gzFiles in file.namelist():
zFile=file.getinfo(file)
return getLog(zFile)
return getLog(inFile)
print inFile
return getLog(inFile)
if type.lower() == '.zip':
if file == None:
with zip.ZipFile(filepath,'r'):
zFile = zip.ZipFile(filepath)
else:
with zip.ZipFile(file,'r'):
zFile = zip.ZipFile(file)
for f_list in zFile.namelist():
content= io.BytesIO(zFile.read(f_list))
pattern = r'(.+)(?P<file>\.zip|\.gz)'
rgz = re.compile(pattern, re.IGNORECASE)
m = rgz.match(f_list)
if m==None:
type = '.log'
decompress_file(f_list,type,zFile)
else:
decompress_file(f_list,m.group('file').lower(),content)
return result
return result
if __name__ == '__main__':
decompress_file("filepath",'.zip' )
Related
The user wishes to have a database backup and restore functionality through the application. They want to be able to download the database file and upload it to restore the database whenever needed. The problem is that django is already running the current DB file. I wrote the following logic to restore the database.
folder ='./'
if request.method == 'POST':
myfile = request.FILES['file']
fs = FileSystemStorage(location=folder)
if myfile.name.split(".")[1] != "sqlite3":
return JsonResponse({"res":"Please upload a database file."})
if os.path.isfile(os.path.join(folder, "db.sqlite3")):
os.remove(os.path.join(folder, "db.sqlite3"))
filename = fs.save("db.sqlite3", myfile)
file_url = fs.url(filename)
return JsonResponse({"res":file_url})
I get the following error which is rightly justified:
[WinError 32] The process cannot access the file because it is being used by another process
So, is there a way I can achieve this functionality through my application?
I found a better way to create this functionality using a csv. One could store the data from the DB into a csv file and restore it when uploaded. Following is my implementation:
def back_up_done(request):
tables = getTableNames()
sql3_cursor = connection.cursor()
for table in tables:
sql3_cursor.execute(f'SELECT * FROM {table}')
with open('output.csv','a') as out_csv_file:
csv_out = csv.writer(out_csv_file)
for result in sql3_cursor:
csv_out.writerow('Null' if x is None else x for x in result)
csv_out.writerow("|")
BaseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
csv_path = os.path.join(BaseDir, 'output.csv')
dbfile = File(open(csv_path, "rb"))
response = HttpResponse(dbfile, content_type='application/csv')
response['Content-Disposition'] = 'attachment; filename=%s' % 'backup.csv'
response['Content-Length'] = dbfile.size
os.remove(os.path.join("./", "output.csv"))
return response
def back_up_restore(request):
if request.method == 'POST':
DBfile = request.FILES['file']
cursor = connection.cursor()
if DBfile.name.split(".")[1] != "csv":
messages.add_message(request, messages.ERROR, "Please upload a CSV file.")
return redirect('back-up-db')
tables = getTableNames()
i = 0
deleteColumns()
decoded_file = DBfile.read().decode('utf-8').splitlines()
reader = csv.reader(decoded_file)
for row in reader:
if len(row) != 0:
if(row[0] == "|"):
i += 1
else:
query = f'''Insert into {tables[i]} values({concatRowValues(row)})'''
cursor.execute(query)
connection.close()
messages.add_message(request, messages.SUCCESS, "Data Restored Successfully.")
return redirect('login')
def concatRowValues(row):
data = ''
for i,value in enumerate(row):
if value == "False":
value = '0'
elif value == "True":
value = '1'
if i != len(row) - 1:
data = f'{data}{str(value)},' if value == "Null" else f'{data}\'{str(value)}\','
else:
data = f'{data}{str(value)}' if value == "Null" else f'{data}\'{str(value)}\''
return data
Where getTableNames and concatRowValues are helper functions to get the names of tables and to concatenate column values to form an executable sql statement, respectively. The "|" character is used to mark the end of a table's data.
i have a project with django .on the host when i want to upload an image sometime error occurred(problem with specific images)! the below show how i resize uploaded images:
def save_files_to_media(request, is_public=False, klass=None, conversation=None):
from apps.file.models import File
fs = FileSystemStorage()
file_items = {}
for data_item in request.data:
file_match = re.search('^fileToUpload\[(\d+)\]$', data_item)
if file_match and file_match.groups():
item_index = file_match.groups()[0]
if item_index not in file_items:
file_items[item_index] = {}
file_items[item_index]['file_to_upload'] = request.data[data_item]
else:
optimize_match = re.search('^optimizeType\[(\d+)\]$', data_item)
if optimize_match and optimize_match.groups():
item_index = optimize_match.groups()[0]
if item_index not in file_items:
file_items[item_index] = {}
file_items[item_index]['optimize_type'] = request.data[data_item]
files = []
for file_item_key in file_items:
input_file = file_items[file_item_key]['file_to_upload']
# TODO: checking validation. if input_file.name is not exist
optimize_type = file_items[file_item_key].get('optimize_type')
file_uuid = str(uuid4())
if is_public:
orig_filename, file_ext = splitext(basename(input_file.name))
directory_name = join(settings.MEDIA_ROOT, file_uuid)
filename = file_uuid + file_ext
else:
directory_name = join(settings.MEDIA_ROOT, file_uuid)
mkdir(directory_name)
filename = input_file.name
filepath = join(directory_name, filename)
fs.save(filepath, input_file)
is_optimized = False
if optimize_type == 'image':
is_success, filepath = image_optimizer(filepath)
filename = basename(filepath)
is_optimized = is_success
file_obj = File(
orig_name=filename,
uuid=file_uuid,
md5sum=get_md5sum(filepath),
filesize=get_filesize(filepath),
meta=get_meta_info(filepath),
is_optimized=is_optimized,
creator=request.user
)
if is_public:
file_obj.is_public = True
else:
file_obj.klass = klass
file_obj.conversation = conversation
file_obj.save()
files.append(file_obj)
return files
here is the error i got with some images:
unsupported Unicode escape sequence
LINE 1: ..., 'ada90ead20f7994837dced344266cc51', 145216, '', '{"FileTyp...
^
DETAIL: \u0000 cannot be converted to text.
CONTEXT: JSON data, line 1: ...ecTimeDigitized": 506779, "MakerNoteUnknownText":
its funny that in my local but not in host. for more information i must tell you guys my postgreSQL version is 11.3 and host postgreSQl is 9.5.17 . where you think is problem? as error it's seems for postgreSQL. thank you
i have tried to take the input phrase and synthesise it with the .wav files given in the folder. if the input phrase has a comma, then it will start after 300ms, if we have a period or question mark, it will start after 400ms.
Started with this code, I don't have an idea how to initiate, join the phrase and play the wav files.
parser.add_argument('--play', '-a', action="store", default=False)
args = parser.parse_args()
class Synthesis(object):
def __init__(self, wav_folder):
self.phones = {}
self.get_wavs(wav_folder)
def get_wavs(self, wav_folder):
for root, dirs, files in os.walk(wav_folder, topdown=False):
for file in files:
filesplit = file.split('.')
filename = filesplit[0]
self.phones[filename] = AS.Audio()
self.phones[filename].load(root + '/' + file)
def punctuation(phrase):
concate_punc = []
for word in phrase:
phn_ex= re.match(r'[a-z]+\|,|[.]|!|[?]', word)
phone_exception = phn_ex.group(0)
print phone_exception
if __name__ == "__main__":
S = Synthesis(wav_folder=args.phones)
output = AS.Audio(rate=48000)
Profesor1= "Profesor-Materia.txt"
Profesor2= "Profesor-Año.txt"
input ("Seleccione un profesor: ")
if(input=="Profesor1"):
file = open(Profesor1)
data1= file.readlines(1)
print(data1)
else:
file = open(Profesor2)
data = file.readlines(1)
print(data)
So this is my code, I want to open The file: "Profesor-Año" whenever I input anything else than "Profesor1" but it just keeps opening the file "Profesor-Materia" even when I input something like: sadsadsad
Can you help me with this problem?
Ps: I've already tried using if(input==Profesor1)
Your code has quite a few problems. Here is a proper version:
Profesor1 = "Profesor-Materia.txt"
Profesor2 = "Profesor-Año.txt"
in_text = str(input("Seleccione un profesor: "))
if(in_text != 'Profesor1'):
file = open(Profesor2, 'r')
data = file.readlines()
else:
file = open(Profesor1, 'r')
data = file.readlines()
print(data)
Your if statement is just checking that the variable Profesor1 has something in it (see the Python docs on truth value testing), which it does. The user input is being ignored.
You need to change it to something like this:
prof = input ("Seleccione un profesor: ")
if(prof == Profesor1):
# do stuff
def _tarFiles(filepaths):
print "create tar file from all files in file list and save to temp working dir. returns tarfile path "
try:
savePathDir = settings.TAR_FILE_TARGET_DIRECTORY
if not os.path.exists(savePathDir):
os.makedirs(savePathDir)
tarredfiles = tarfile.open(settings.TAR_FILE_TARGET_DIRECTORY + '/' + 'responsefiles.tar',mode='w')
for f in filepaths:
tarredfiles.add(f)
tarredfiles.close()
return ("Ok", settings.TAR_FILE_TARGET_DIRECTORY + '/' + 'responsefiles.tar')
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " + e.message, None)
def sendFiles(files):
try:
result, tarfilename = _tarFiles(files)
if result == 'Ok':
try:
print tarfilename
wrapper = FileWrapper(file(tarfilename))
response = HttpResponse(wrapper, content_type='application/x-tar') #zip,avi,png,jpeg, etc...
response['Content-Disposition'] = 'attachment; filename=' + tarfilename#tarredfiles.name #eg. myfile.zip
response['Content-Length'] = str(os.path.getsize(tarfilename))
return ("Ok",response)
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " + e.message, None)
else:
return (result,None)
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " +e.message,None)
tarfilename is the complete path to the file.
The content-length looks right (comparing actual file to getsize).
Works on a mac running runserver. Returns a partial file on windows running runserver. Or completely empty file if I step through on windows.
The target directory and filename generated is "tarred_files/responsefiles.tar"
the file size is 90K and the os.path.getsize returned is 92160
What am I doing that would cause an empty file to be downloaded?
For windows, you need to add "rb" to file.
like so:
wrapper = FileWrapper(file(tarfilename,'rb'))
Also, Content-Length should use a integer, not a string
like so:
response['Content-Length'] = os.path.getsize(tarfilename)