Python shutil.copy fails on FAT file systems (Ubuntu)
Well I cheat in this case.
If I know that the target is a file system where chmod
fails, I simply delete the chmod
method from the os package using del os.chmod
, and this allows the copy to succeed.
>>> import os
>>> print hasattr(os, 'chmod')
True
>>> foo = os.chmod
>>> del os.chmod
>>> print hasattr(os, 'chmod')
False
This now allows you to perform the copy without failing on the chmod. Then we re-enable it by assigning the attribute back.
>>> setattr(os, 'chmod', foo)
>>> print hasattr(os, 'chmod')
True
Using Python shutil.copy to overwrite a file gives asyntax error
Problem solved- turns out the error was incorrect indentation in an if-else earlier in the script- corrected that and the syntax error went away.
shutil.copy() function got error message FileNotFoundError
I tried the code and it's working perfectly, you have to right click
in your home
folder -> new
-> text document
and name it spam
, then the some_folfer
will be created with a copy of the content of spam.txt
How do I copy an entire directory of files into an existing directory using Python?
This limitation of the standard shutil.copytree
seems arbitrary and annoying. Workaround:
import os, shutil
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
Note that it's not entirely consistent with the standard copytree
:
- it doesn't honor
symlinks
andignore
parameters for the root directory of thesrc
tree; - it doesn't raise
shutil.Error
for errors at the root level ofsrc
; - in case of errors during copying of a subtree, it will raise
shutil.Error
for that subtree instead of trying to copy other subtrees and raising single combinedshutil.Error
.
python shutil copy the files from source dir to remote dir based on condition
Here is a fix of your existing script. This doesn't yet attempt to implement the "source newer than target" logic since you didn't specifically ask about that, and this is arguably too broad already.
for filename in glob.glob("/data/{0}/*/*.txt".format(Info_month)):
# The result of the above glob _is_ a full path
if os.path.getsize(filename) > 0:
# Minor tweak: use os.path.join for portability
if not os.path.exists(os.path.join(["/remote/data/", os.path.basename(filename)])):
shutil.copy(filename, "/remote/data/")
# no need for an explicit "else" if it's a no-op
how to copy many files inside of a directory to many other directories using shell or python
The following code will copy all your original_dir files to the destination_dir.
I believe you can do some customized changes to get what you want.
import os
from shutil import copyfile
original_dir = "/path/to/the/original/dir"
original_files = os.listdir(original_dir)
destination_dir = "/path/to/your/destination/dir"
for each in original_files:
current_file = os.path.join(original_dir, each)
# check if it is a file
if not os.path.isdir(current_file):
# print(each + " is being processing")
copyfile(current_file, destination_folder + each)
Related Topics
Pip Is Not Working for Python 3.10 on Ubuntu
How to Create a User in Linux Using Python
Multiple Instances of Python Running Simultaneously Limited to 35
What Is the Easiest Way to Detect Key Presses in Python 3 on a Linux MAChine
Python Convert Microsoft Office Docs to Plain Text on Linux
Allowing Ctrl-C to Interrupt a Python C-Extension
Python: Interplay Between Lib/Site-Packages/Site.Py and Lib/Site.Py
Linux:Python:Clear Input Buffer Before Raw_Input()
Python's Equivalent for R's Dput() Function
Auto.Arima() Equivalent for Python
Converting Python Objects for Rpy2
Which Command to Use for Checking Whether Python Is 64Bit or 32Bit
How to Send Input on Stdin to a Python Script Defined Inside a Makefile