I am trying to copy some existing files (using full file paths) into a zip folder that I have created using the Julia Language.
So far I have tried both ZipFile.jl and InfoZIP and neither package has been able to add existing files to the zip folder, just create new one to write into. If anyone has any tips for how to generate a new zip folder and fill it with pre-existing files I would greatly appreciate it.
Thanks
Basically you can read each file and write it into the archive :
w = ZipFile.Writer("archive.zip")
for filepath in filelist
f = open(filepath, "r")
content = read(f, String)
close(f)
zf = ZipFile.addfile(w, basename(filepath));
write(zf, content)
end
close(w)
Related
I have a list of dataframes (df_cleaned) created from multiple csv files chosen by the user.
My objective is to save each dataframe within the df_cleaned list as a separate csv file locally.
I have the following code done which saves the file with its original title. But I see that it overwrites and manages to save a copy of only the last dataframe.
How can I fix it? According to my very basic knowledge perhaps I could use a break-continue statement in the loop? But I do not know how to implement it correctly.
for i in range(len(df_cleaned)):
outputFile = df_cleaned[i].to_csv(r'C:\...\Data Docs\TrainData\{}.csv'.format(name))
print('Saving of files as csv is complete.')
You can create a different name for each file, as an example in the following I attach the index to name:
for i in range(len(df_cleaned)):
outputFile = df_cleaned[i].to_csv(r'C:\...\Data Docs\TrainData\{0}_{1}.csv'.format(name,i))
print('Saving of files as csv is complete.')
this will create a list of files named <name>_N.csv with N = 0, ..., len(df_cleaned)-1.
A very easy way of solving. Just figured out the answer myself. Posting to help someone else.
fileNames is a list I created at the start of the code to save the
names of the files chosen by the user.
for i in range(len(df_cleaned)):
outputFile = df_cleaned[i].to_csv(r'C:\...\TrainData\{}.csv'.format(fileNames[i]))
print('Saving of files as csv is complete.')
Saves a separate copy for each file in the defined directory.
So, when writing parquet files to s3, I'm able to change the directory name using the following code:
spark_NCDS_df.coalesce(1).write.parquet(s3locationC1+"parquet")
Now, when I output this, the contents within that directory are as follows:
I'd like to make two changes:
Can I update the file name for the part-0000....snappy.parquet file?
Can I output this file without the _SUCCESS, _committed and _started files?
The documentation i've found online hasn't been very helpful.
out_file_name = snappy.parquet
path = "mnt/s3locationC1/"
tmp_path = "mnt/s3locationC1/tmp_data"
df = spark_NCDS_df
def copy_file(path,tmp_path,df,out_file_name):
df.coalesce(1).write.parquet(tmp_path)
file = dbutils.fs.ls(tmp_path)[-1][0]
dbutils.fs.cp(file,path+out_file_name)
dbutils.fs.rm(tmp_path,True)
copy_file(path,tmp_path,df,out_file_name)
This function copy and paste your required output file to the destination and then delete the temp files, all the _SUCCESS, _committed and _started removed with it.
If you need anything more, please let me know.
I know how to do this for one archive at a time, but I want to add files, to multiple archives, in the same folder, simultaneously; if that is possible. I understand that I can do this with a batch file... but I don't know how to write the script / text.
So... I have several zip files in one folder. I want to add a specific text file and a specific image file to each/all of those zips. I don't want any other modifications of the zip files.
Or... is there a way to set WinRAR so that specific files will be automatically added whenever an archive is created?
Thanks
import zipfile
z = zipfile.ZipFile('cal.zip', mode='a', compression=zipfile.ZIP_DEFLATED)
z.write('/your/file/path') # or, z.writestr('your-filename', 'file-content')
z.close()
I am creating a zip file with JSZip but I have a very specific requirement that I can't figure out how to do.
When this file is unzipped it needs to create a folder with two files inside it. Let's say foo.zip unzips to create a folder foo with files bar.mp4 and baz.png.
So far that's easy enough I can just go
let zip = new Zip()
let folder = zip.folder('foo')
folder.file(`bar.mp4`, videoData)
folder.file(`baz.png`, imageData)
// Save as foo.zip
But what I need to happen is if I rename foo.zip to horse.zip then the folder created should be called horse. If I use the above code then it will always be called foo. I know this is possible because I have several zip files that currently do this.
Edit: I'm not tied to JSZip here either. I'm happy to do this any way that's possible. Code will only be run on the server.
How would I add the contents of an entire directory to an already existing zip file using python? The directory to be added to the zip file will also include other folders as well and there will be duplicates in the zip file that will need to be overwritten. Any help would be appreciated. Thanks in advance!
P.S. If it is possible to zip the directory then combine both files that would also work.
Python's zipfile module allows you to manipular ZIP compressed archives. The ZipFile.namelist() method returns a list of files in an archive, and the ZipFile.write() method lets you add files to the archive.
z = zipfile.ZipFile('myfile.zip')
The os.walk method allows you to iterate over all the files contained in a directory tree.
for root, dirs, files in os.walk('mydir'):
for filename in files:
z.write(os.path.join(root, filename))
Replacing a file in an archive appears to be tricky; you can removed items by creating a temporary archive and then replacing the original when you're done as described in this question.
It might be easier just to call the zip command instead, but put these together and you should be able to get to where you want.