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.
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.
import tempfile
storage_path = os.path.join(tempfile.gettempdir(), 'storage.data')
with open(STORAGE_PATH, 'w') as f:
f.write(data)
In which directory will the file be created? And is it possible to see it there or is it invisible?
You only created a string, nothing more.
The tempfile.gettempdir() function returns a string:
Return the name of the directory used for temporary files
It's just a name. Print it if you need to know the location on your system but don't want to step through the rules outlined in the documentation; there are 3 environment variables and another 3 standard locations to test to see where the current user has permission to create files.
Next, os.path.join() takes strings and outputs a string. Nothing is created on disk to build these strings. It'll depend on whether or not you actually do something with that string (like calling os.makedirs() or open()) on what will be created.
If you use a plain open() call on that string then a regular file is created in the tempfile.gettempdir() location that is visible to other processes.
If you need to create a temporary file securely, so in a manner that an attacker can't access the file or influence where it is created, use the tempfile.TemporaryFile() constructor or the tempfile.mkstemp() function; on UNIX systems that'll result in a file that is not listed in the directory any more (the file exists but can't be opened by other processes). It will be created in the tempfile.gettempdir() location however, so it'll use up disk space on the relevant disk partition that that directory lives on.
From this link...
The list is:
the directory named by the TMPDIR environment variable
the directory named by the TEMP environment variable
the directory named by the TMP environment variable
a platform-specific location:
on Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order.
on all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.
as a last resort, the current working directory.
A sample from mac os :
/var/folders/4x/2rjwjpyn1pn9_0t1r5_m8blr0000gn/T/storage.data
When you go to the directory via finder the file should be there. As for Windows it might be hidden which I believe you can unlock.
There is currently no file being created. Check out the tempfile documentation to find more information about functions of the module such as the .gettempdir():
Python searches a standard list of directories to find one which the
calling user can create files in. The list is:
The directory named by the TMPDIR environment variable.
The directory named by the TEMP environment variable.
The directory named by the TMP environment variable.
A platform-specific location:
On Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order.
On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.
As a last resort, the current working directory.
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