Questions tagged [shutil]

A Python module which contains a number of utility methods for file or directory operations, such as copying, moving, etc.

The Python shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.

Documentation: https://docs.python.org/3.2/library/shutil.html

1181 questions
4
votes
2 answers

move files python according value columns

I would like to move some files in python according to some values on python dataframe. My script already creates the folders and subfolders necessary but I want to move the files(each row in my df is a file/document) to those folders/subfolders. I…
Enterrador99
  • 121
  • 1
  • 13
4
votes
1 answer

Unexpected file using shutil.make_archive to compress file

I am trying to use shutil.make_archive to zip files. When I am using : import shutil shutil.make_archive('zip_file', 'zip', 'C:\\Users\\test') I got a zip_file in the zip_file, and the zip_file in the zip_file seems to be a corrupted file. Anyone…
Drake .C
  • 332
  • 4
  • 16
4
votes
3 answers

Python: move a file up one directory

Below is a small test routine which takes a path to a file and moves the file up one directory. I am using the os and shutil modules, is there one module that could perform this task? Is there a more pythonic way of implementing this functionality?…
10SecTom
  • 2,484
  • 4
  • 22
  • 26
4
votes
1 answer

shutil make_archive resulting in nested .zip files

I've been trying to use make_archive from shutil library. Here is the code (with comprehensive comments): from shutil import make_archive make_archive( 'zipfile_name', 'zip', # archive format root_dir=None, # current working dir…
scharette
  • 9,437
  • 8
  • 33
  • 67
4
votes
3 answers

What is a good solution to a bogus OSError, 13 (EACCES) using Python on Windows

Here is the code: def make_dir(dir_name): if os.path.exists(dir_name): shutil.rmtree(dir_name) try: os.makedirs(dir_name) except OSError, e: print "ErrorNo: %s (%s)" % (e.errno, errno.errorcode[e.errno]) raise IFF the directory already…
David Rogers
  • 4,010
  • 3
  • 29
  • 28
4
votes
3 answers

Python: Should I use shutil or subprocess to manipulate files and directories as a better approach?

I'm a beginner in Python with a shell scripting background. I have learned shutil and subprocess modules to create files/directories. My question is, which one is better and which of them is recommended to be used for managing files in my OS…
Linux Cli Aik
  • 91
  • 2
  • 6
4
votes
2 answers

Is shutil.make_archive thread safe?

I am trying to zip multiple folders using shutil.make_archive using threads in python. I see that the smaller folder zips completely and at the same time the other thread also stops zipping. So, is shutil.make_archive thread safe?
Praveen S
  • 104
  • 11
4
votes
2 answers

shutil samefile error on Linux

I have a python script that is giving me a hard time on Ubuntu 12.02 with Python 2.7.3. PS: it runs without problems on Windows. >>> import os >>> import shutil >>> shutil.copy("/mnt/my_network_dive/somewhere/sample.xml",…
vmenezes
  • 1,076
  • 2
  • 12
  • 21
4
votes
1 answer

Python throws error when deleting a directory that is open with windows explorer

I'm writing a code in Python, in which I check if a certain folder exists; if it does, I remove it and create a new one (with the same name). The code is as follows: if os.path.exists(output_folder): shutil.rmtree(output_folder) …
Cheshie
  • 2,777
  • 6
  • 32
  • 51
4
votes
1 answer

using shutil.copytree without copystat

I am trying to write a python script backing up a folder, and keeping it for x days. I use shutil.copytree(source, finaldest) My problem is, the timestamp from the original files persists, meaning the folders will be deleted if the files…
Bok
  • 537
  • 5
  • 21
4
votes
1 answer

Find files by creation/modification date then moving to another dir in Python

First question. I am new to programming, much less python. As the title says I am attempting to find files that were created or modified in the past 24 hours, then move those files to another directory. I can find the files but I can't figure out…
MisterToe
  • 43
  • 1
  • 5
4
votes
0 answers

Why cant I copy multiple files into one directory?

I am trying to copy multiple files from one directory, into another directory. src_files = os.listdir("srcdir") print(src_files) for file_name in src_files: full_file_name = os.path.join("srcdir", file_name) if…
Sam Coulter
  • 708
  • 1
  • 6
  • 27
4
votes
0 answers

Why is shutil.copy2 so much slower than cp -p?

I'm moving a whole bunch of files from one place to another, some of them pretty large .wav files, and changing around the directory structure at the destination, so I couldn't copy directories wholesale. I originally was using the copyFile function…
JoFrhwld
  • 8,867
  • 4
  • 37
  • 32
4
votes
2 answers

shutil.move(scr, dst) gets me IOError: [Errno 13] Permission denied and 3 more errors

Documents = ['*pdf', '*docx', '*txt'] for i in range(len(Documents)): if glob.glob(Documents[i]): print(Documents[i], True) shutil.move(glob.glob(Documents[i])[0], '/home') else: print(Documents[i], False) Well,…
Joao Guedes
  • 51
  • 1
  • 5
4
votes
3 answers

append contents from one file to another with newline separation

I'm trying to, I think, replicate the cat functionality of the Linux shell in a platform-agnostic way such that I can take two text files and merge their contents in the following manner: file_1 contains: 42 bottles of beer on the wall file_2…
glarue
  • 530
  • 7
  • 20