shutil

Python read/write vs shutil copy

Python read/write vs shutil copy Question: I need to save files uploaded to my server (Max file size is 10MB) and found this answer, which works perfectly. However, I’m wondering what is the point of using the shutil module, and what is the difference between this: file_location = f"files/{uploaded_file.filename}" with open(file_location, "wb+") as file_object: file_object.write(uploaded_file.file.read()) …

Total answers: 2

Python [Errno 22] Invalid argument when copy a file from one folder to another

Python [Errno 22] Invalid argument when copy a file from one folder to another Question: I am using the files from a video tutorial. At the beginning, it starts to spread the files of input image data by copying them in various folders. The code works in the tutorial but I wonder why I get …

Total answers: 2

Why is Python's shutil.which() not working?

Why is Python's shutil.which() not working? Question: I’m trying to see if shutil.which() works for finding the blastn command from NCBI’s BLAST. Running which blastn on my terminal results in /usr/local/bin/blastn. However, if I do shutil.which(“blastn”), it simply returns None. Searching for Python works fine, as shutil.which(“python”) returns /usr/bin/python. Why is this happening? Asked By: …

Total answers: 2

Deleting folder in python in the background

Deleting folder in python in the background Question: The standard way of deleting folders in python I am aware of is shutil.rmtree(‘/path/to/folder’) However, this command blocks until the deletion is completed, which in the case of large folders can take a long time. Is there a non-blocking alternative? I.e. a function that would delete the …

Total answers: 1

Python Pathlib path object not converting to string

Python Pathlib path object not converting to string Question: I am trying to use Shutil to copy a pdf file using path objects from Pathlib, however when I run my code I get the error “str object is not callable” when converting my paths back to strings using str(). Any explanation for why this is …

Total answers: 1

Python: How to move list of folders (with subfolders) to a new directory

Python: How to move list of folders (with subfolders) to a new directory Question: I have a directory that literally has 3000+ folders (all with subfolders). What I need to be able to do is to look through the directory for those files and move them to another folder. I’ve done some research and I …

Total answers: 1

Empty/blank result file when using shutil to copy a tempfile

Empty/blank result file when using shutil to copy a tempfile Question: import tempfile import shutil tmp_f = tempfile.NamedTemporaryFile(delete=False) tmp_f.write("hello world") shutil.copy(tmp_f.name, "/etc/exports") When I read /etc/exports, it is a completely empty file. what is wrong? Asked By: pepero || Source Answers: You need to close the file: tmp_f.write(“hello world”) tmp_f.close() Answered By: j4hangir In addition …

Total answers: 2

writing StringIO back to disk in python

writing StringIO back to disk in python Question: I created an in-memory zip file using StringIO and zipfile: inMemoryZip = StringIO() outfile = zipfile.ZipFile(inMemoryZip, ‘w’, compression=zipfile.ZIP_DEFLATED) //outfile.write(stuff) inMemoryZip.seek(0) return inMemoryZip This data is uploaded to a server/database. At some point, it’s retrieved and I need to write it to disk as a zip file. I’m …

Total answers: 1

Compressing directory using shutil.make_archive() while preserving directory structure

Compressing directory using shutil.make_archive() while preserving directory structure Question: I’m trying to zip a directory called test_dicoms to a zip file named test_dicoms.zip using the following code: shutil.make_archive(‘/home/code/test_dicoms’, ‘zip’, ‘/home/code/test_dicoms’) The problem is that when I unzip it, all of the files that were in /test_dicoms/ are extracted to /home/code/ instead of the folder /test_dicoms/ …

Total answers: 8