Python Shutil.Copy Fails on Fat File Systems (Ubuntu)

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 and ignore parameters for the root directory of the src tree;
  • it doesn't raise shutil.Error for errors at the root level of src;
  • 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 combined shutil.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



Leave a reply



Submit