I have a subdirectory "update" and I need to move all files and folders from it to the current directory, removing all files and folders from the current directory. The problem is that there are a lot of subfolders in the subdirectory and I can't figure out how to move them all.
shutil.move recursively moves files and folders.
If you wish to copy the internal directory and extract everything, use the following line:
shutil.copytree("update", ".", dirs_exist_ok=True)
You may then safely remove the "update" directory:
shutil.rmtree("update")
If you wish to clear the current directory, I would first move the update directory outside the current one, delete the current, and then rename.
Keep in mind, that if it's your current directory, I'm guessing the directory would be in use as Python is from it, so you can't really delete that.
Related
I want do delete a directory with python. I used this:
import shutil
shutil.rmtree('path/to/dir', ignore_errors=True)
but when I go into the folder, there is still a folder.
(Its a .git folder which has content)
How can I also delete this directory too?
I already searched and tested many other posts but all didn't work like it should.
I have a directory with some files in it (they're mostly images with a JSON file). I want to process those files, possibly overwrite some of them and possibly create some new files from some other source. I can have an error happen at any point in this process.
How do I ensure that if an error occurs as I'm processing one of the files that I won't end up with the directory in a weird state? I want to only change the contents of the directory if everything went well.
Should I create a temporary directory with tempfile.mkdtemp, put my code in a try, do my update in the "temporary" directory, swap the existing directory with the temporary directory, and delete the temporary directory if it still exists in the finally?
I'm using Python (Django).
I am very new to python and stack overflow. In a directory, I have a list of many sub directories with many files within each. I am trying to figure out a way to find the largest file within each sub directory, and copy (move) these files to a new directory. Is this possible, any help is much appreciated.
You'll want to look at the os module, and the os.path submodule in that. os.listdir and os.path.getsize will both be useful. You can use os.rename to move a file, or, for maximum compatibility, can use the shutil module's shutil.move, which will help if you're moving things from one filesystem to another.
I'm using a module that saves screen shots into my Python34 folder as .png files.
I want the .png files to be relocated into a separate folder within the Python34 folder.
I want this to be done automatically. I was thinking maybe I could loop through the Python34 folder to find all the .png files and then save these files into a new "PNG" folder within the Python34 folder, but I don't know how I would do this. How should I approach this problem?
Some clarifications:
The module I am using is called "Desktopmagic", ( https://pypi.python.org/pypi/Desktopmagic/13.3.29 )
I am working with Windwows.
As I can see from the code of Lib\site-packages\desktopmagic\screengrab_win32.py:getDCAndBitMap (called from saveScreenToBmp), the module uses the provided file name argument as is.
I.e. if you specify it without a path, it saves into the current directory. So, if you wish to save elsewhere, either change it or specify a path (relative or absolute) in the argument. Btw, saving random files under the python installation dir is a very bad idea.
If you want to prepend a path by default, you'll need to use a wrapper or replace some of the module's machinery.
You can search a directory for a certain set of files using glob, for example
import glob
screenshots = glob.glob('*.png')
You can then copy the files by looping over the screenshots list and using shutil.copyfile
I have two source file paths:
C:\Same\Path\To\File\unknown\
C:\Same\Path\To\File\unrecognized\
And one destination path:
C:\Same\Path\To\File\Import
The subfolders for all these paths are the same. They are 10-15 folders that have 3 digit names (233 for example).
If any files are in these subfolders of the source paths I want to CUT them from that directory and send them to the destination directory with the same path as the source. i am new to python so any help would be appreciated.
I think what you want is shutil.move (py3k docs):
Help on function move in module shutil:
move(src, dst)
Recursively move a file or directory to another location. This is
similar to the Unix "mv" command.
If the destination is a directory or a symlink to a directory, the source
is moved inside the directory. The destination path must not already
exist.
If the destination already exists but is not a directory, it may be
overwritten depending on os.rename() semantics.
If the destination is on our current filesystem, then rename() is used.
Otherwise, src is copied to the destination and then removed.
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.
Look into the shutil module.