As per the code below I am having issues with the zipping a directory using the python 3 shutil.make_archive function. The .testdir will be zipped but it is being zipped in /home/pi, instead of /home/pi/Backups.
zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(zip_loc, 'zip', zip_dest)
Could anyone explain what I am doing wrong?
Reading the docs here I came up with:
zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(base_dir=zip_loc, root_dir=zip_loc, format='zip', base_name=zip_dest)
From the docs:
base_name is the name of the file to create, including the path, minus any format-specific extension.
root_dir is a directory that will be the root directory of the archive; for example, we typically chdir into root_dir before creating the archive.
base_dir is the directory where we start archiving from; i.e. base_dir will be the common prefix of all files and directories in the archive.
root_dir and base_dir both default to the current directory.
Before to write the archive, move to the good directory :
old_path = os.getcwd()
os.chdir(path)
-> write the archive
After writing the archive move back to old directory :
os.chdir(old_path)
Related
How can I use Python to create new folders relative to my current working directory?
For example, my path is C:/Documents/Code with no folders within and just has my Python file. How do I store some data within C:/Documents/Code/Data without hard coding the absolute path?
This is what I've been trying:
path = "/Data/file.txt"
file = open(path, "w")
This gives me the error of "No such file or directory".
Thanks for any assistance!
Prepend the path with a single dot ., which denotes your current working directory:
path = "./Data/file.txt"
# ^
I have a many folders. I want to make .zip format of files and move them to another directory:
for (dir, dirs, files) in os.walk(directory):
for filename in files:
# make zip files
if filename.endswith(".zip"):
# move created zip files
os.rename(dir + '/' + filename, new + '/' + directory + '/' + filename)
The problem is that, after make the zip file, program couldn't find the created file with zip extension. I want to create a zip format and move it to another folder.
You can use the shutil lib:
import shutil
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
shutil.move(src, dst)
Recursively move a file or directory (src) to another location (dst).
If the destination is an existing directory, then src is moved inside
that directory. If the destination already exists but is not a
directory, it may be overwritten depending on os.rename() semantics.
If the destination is on the current filesystem, then os.rename() is
used. Otherwise, src is copied (using shutil.copy2()) to dst and then
removed.
Also you can use shutil for archive file:
shutil.make_archive(base_name, format[, root_dir[, base_dir[,
verbose[, dry_run[, owner[, group[, logger]]]]]]])
Create an archive
file (such as zip or tar) and return its name.
For more info read shutil documentation
I am opening some VTU files from Directory X and there are other output files in that directory (for example log.txt) that I want to open via a plugin. If I do a os.getcwd() I end up in ParaViews installation directory. What I want is the directory of the VTU files I loaded BEFORE applying the plugin... So basically the start Point of the Pipline.
You could do something like this to get the reader
myreader = FindSource('MyReader')
then get the file name via the FileName attribute
myreader.FileName
I'm using node-archiver to zip some files and in the zip file I find the archived files under the directory where was zipped from.
So in the zip file, 'myfile.txt' is under this sort of directory:
'home/ubuntu/some_folder/another_folder/'
I want to have the files in the root directory of the zip file.
This is the code I suspect that is in charge with this:
archive.bulk(
{src: filePaths}
);
Its documentation is here although I don't see how to do it.
Any ideas?
Just add expand:true, flatten:true and indicate what name you want to use for destination folder (dest: 'destinationfoldername/')
archive.bulk(
{src: filePaths, dest:'destinationfoldername/', expand:true, flatten:true}
);
It workerd for me
I have two folders, one called 'modified' and one called 'original'.
'modified' has no subdirectories and contains 4000 wav files each with unique names.
The 4000 files are copies of files from 'original' except this folder has many subdirectories inside which the original wav files are located.
I want to, for all the wav files in 'modified', replace their name-counterpart in 'original' wherever they may be.
For example, if one file is called 'sound1.wav' in modified, then I want to find 'sound1.wav' in some subdirectory of 'original' and replace the original there with the modified version.
I run Windows 8 so command prompt or cygwin would be best to work in.
As requested, I've written the python code that does the above. I use the 'os' and 'shutil' modules to first navigate over directories and second to overwrite files.
'C:/../modified' refers to the directory containing the files we have modified and want to use to overwrite the originals.
'C:/../originals' refers to the directory containing many sub-directories with files with the same names as in 'modified'.
The code works by listing every file in the modified directory, and for each file, we state the path for the file. Then, we look through all the sub-directories of the original directory, and where the modified and original files share the same name, we replace the original with the modified using shutil.copyfile().
Because we are working with Windows, it was necessary to change the direction of the slashes to '/'.
This is performed for every file in the modified directory.
If anyone ever has the same problem I hope this comes in handy!
import os
import shutil
for wav in os.listdir('C:/../modified'):
modified_file = 'C:/../modified/' + wav
for root, dirs, files in os.walk('C:/../original'):
for name in files:
if name == wav:
original_file = root + '/' + name
original_file = replace_file.replace('\\','/')
shutil.copyfile(modified_file, original_file)
print wav + ' overwritten'
print 'Complete'