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.
Related
I was creating a program that checked for files in my Downloads folder and moved them in some subfolders depending on their extention.
I used os.walk("path") # path is the path to my downloads folder
and printed the files it found, just to see if it worked.
The problem is that the program finds the files in the folder, but it also finds some files that aren't there, most of them end with .zpa, and it also finds a desktop.ini file.
Is this normal or is there something wrong?
os.walk will find all files including those marked with hidden and system attributes. Those files are not displayed by default in the Windows Explorer or the command line dir utility.
If you want to skip hidden and system files in Python you'll have to check for the file attributes:
import os
import stat
for path,dirs,files in os.walk('Downloads'):
for file in files:
filename = os.path.join(path,file)
info = os.stat(filename)
if not info.st_file_attributes & (stat.FILE_ATTRIBUTE_SYSTEM | stat.FILE_ATTRIBUTE_HIDDEN):
print(filename)
See os.walk finds all files inside a directory. Those files which are hidden in Windows file explorer are also found out by this. Hidden files can include
Files hidden by you from within file explorer
Files hidden by OS itself, which are OS specific configuration files which we don't have to worry much about.
Files hidden by any application software intentionally
Probably there can be files from the above category which you got to see in the output.
As far as .zpa files are concerned, I don't know about those, but found a link which could help you.
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.
I have a folder with the following layout:
root/
package0/
__init__.py
main.py
package1/
__init__.py
main.py
Inside the package1/main.pyI have import package0.
When I open a terminal on root folder and run python package1/main.py it works fine. But this is very strange since the cwd was not supposed to be included in path, only the folder in which the script is in, package1 in this case.
When I print the sys.path I can see that the root folder is there.
When I run the same code on my other computer I get a import error as expected.
I cannot understand why I am seeing this behavior.
I have already checked .bashrc and there is no code adding the cwd to the python path.
What might be different on the two computers, I am transferring the root folder from one computer to another through git.
Essentially, whenever you start a particular script, the 'working directory' is the directory from which you started the script. When you run your script from the root folder using command line, the script will look for any files you mention with the root folder as the 'root' of any paths.
Hope that addressed some of your questions. If you're interested in working with changing the starting directory, you can read more about it here.
Edit: Continuation on how to solve changing the working directory for any particular file, this should grab your current running file's directory, change the path to it, and change the directory one higher.
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
os.chdir("..")
I found a solution. I have no idea of why this solved the problem.
I had two folders added to my pythonpath on .bashrc, some TensorFlow stuff, nothing related to this task.
When I commented the line that added these folders, my cwd stopped to be added to the pythonpath. I took a look into those folders but I could not figure out what was causing this.
I am still curious anyway.
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.