I'm writing a script that is extracting 2 JSON files from deep (3 folders down) in a ZIP Folder, modifying its contents, and writing those files back into that same folder. The only issue is that when I write the JSON Files back into the folder where they were initially pulled from, they do not overwrite so now I have 4 files named (product, product, package, package). I wish to delete or overwrite the non-modified files but don't know how.
I have looked up how to do it on other threads and it says it's not supported but I was hoping by now someone had addressed this without having to do the "rewrite the entire directory minus the old files into a new ZIP" routine. I've also tried writing to the zip file with 'w' mode but that just deletes the whole other contents of the zip file.
This is how I'm writing each file back into the ZIP file
zf = zipfile.ZipFile(ZipName, 'a')
zf.write('package.json',addressBook[0],compress_type = zipfile.ZIP_DEFLATED
zf.write('product.json',addressBook[1],compress_type = zipfile.ZIP_DEFLATED
Related
I am looking for an AppleScript that moves files in a single directory into new folders based on info provided in a CSV file, where the columns are A) Filename with extension B) New folder title. Thank you.
I am in the process of uploading a huge number of tests for my school (I am a computer science teacher). These come in the form of .h5p files. I need to parse information into the .h5p files from .txt documents, ready for uploading to Moodle courses. To do this, I have built an app to push the data from .txt files into the .json files in the .h5p file.
The problem is that my app converts the h5p to a zip, unzips it and then parses the information, rezips and then changes the extension again to h5p. Would you mind watching this video https://youtu.be/FTyQddAcWa8 and letting me know how I might be able to edit the .json files and then rezip ready for uploading to the Moodle courses? The files throw up errors once unzipped and then zipped again.
I think the unzipping process is altering the relative links.
Bottom line is, these tests are critical in my school of 1,274 children mitigating the impact of COVID-19 lockdown.
The unzipping process is not the problem, but the zipping is.
When you upload the file, H5P is complaining because it expects some flags to be set when zipping:
-D do not add directory entries
-X eXclude eXtra file attributes
I assume that at some point your script is calling zip. That call would need to pass the correct flags. On a command line, you'd use
zip -rDX myNewFile.h5p *
to pack all files in the current directory into a valid H5P content file named myNewFile.h5p. Just "translate" that into your script.
How can I save two files with the same file name in the same folder without renaming anyone using php?
For instance: A user has an audio file name "first.mp3"; and another user uploads another file named: "first.mp3"; and I want to save these two files without renaming any so that when people are downloading the audio from the front end, the name does not change.
I can do this by concatenating a random number to differentiate the files but I want to beat this method of renaming.
Should I be saving each file inside a unique folder and save the file names to database? but this method will create too many folders which i don't think it is appropriate.
You cannot have two files with the same name in the same folder.
You would either have to add a random string to the end of each file like you suggest or save each user's files in a directory allocated to their account.
Regards,
Leslie
Saving multiple files with the same name within the same folder is just not possible.
I'd opt for a strategy that would involve saving the original filename somewhere (in a database, for example) along with the name/path of the actual file. When the User downloads the file (presumably through a web app of some sorts), you can set the name of the file via headers with your language of choice.
You could even rename the files to something completely random when they're uploaded so you can have them all in one folder - as long as you store the original filename somewhere, you can always set it before you serve it back to the end user.
my question is how we will create SSIS package to get the particular file from multiple files and copy to different folder
Need Help
You can use File System Task in your package control flow.
Configure the properties as below:
DestinationConnection: Enter the full file path of the folder you want to move your file to e.g c:\Users\ToTest
Operation: Change to Move file.
SourceConnection: Enter the full file path of the folder where your file is, including the file name and extension e.g c:\Users\Test\testfile.csv
You'll need at least two components.
Use a Foreach Loop container with the default Foreach File Enumerator on the Collection tab. Set Folder: to \YourFolder and build an expression under Files: to identify the particular file you're interested in.
Inside the Foreach Loop container, add a File System Task to Copy the file from the Source folder to the Destination folder.
I am creating a Zip file from a folder (and subfolders). it works fine and creates a new .zip file also but I am having an issue while using glob.glob. It is reading all files from the desired folder (source folder) and writing to the new zip file but the problem is that it is, however, adding subdirectories, but not adding files form the subdirectories.
I am giving user an option to select the filename and path as well as filetype also (Zip or Tar). I don;t get any problem while creating .tar.gz file, but when use creates .zip file, this problem comes across.
Here is my code:
for name in (Source_Dir):
for name in glob.glob("/path/to/source/dir/*" ):
myZip.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
myZip.close()
Also, if I use code below:
for dirpath, dirnames, filenames in os.walk(Source_Dir):
myZip.write(os.path.join(dirpath, filename) os.path.basename(filename))
myZip.close()
Now the 2nd code taks all files even if it inside the folder/ subfolders, creates a new .zip file and write to it without any directory strucure. It even does not take dir structure for main folder and simply write all files from main dir or subdir to that .zip file.
Can anyone please help me or suggest me. I would prefer glob.glob rather than the 2nd option to use.
Thanks in advance.
Regards,
Akash
Glob by design does not expand into subdirectories. It follows UNIX style path rules and expansions see the documentation for fnmatch for more information. If you want to get at the subdirectories you need to add it to the path. This example will get everything at one level down.
for name in (Source_Dir):
for name in glob.glob("/path/to/source/dir/*/*" ):
myZip.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
myZip.close()
Doug Hellman has an excellent discussion here. If you are not using the pattern features of glob (like *.txt for all text files or *[0-9].txt for all text files that have a number before the extension) then I think your os.walk solution is better