Copy and replace file python

How can I replace a file if already exists in the destination folder?

I just want to move a file from one folder to another (already know how to do that) and in the process check all the files in the destination folder and delete the files with the same name. I have two folders /src and /dst. In folder /src I have:

  • ‘access.log.1.20171110_115840565311.txt’
  • ‘access.log.1.20171110_115940565311.txt’
  • ‘access.log.2.20171110_115940565311.txt’

When I move the file in /src to /dst I want to delete all the files named as the file in /src excluding the datetime() extension in the files of /dst.

So the /dst folder should look like this after the execution:

This is the code I have to move files from /src to /dst:

entrada = ENTRADA #This 3 are the paths to the folders /src salida = SALIDA # /dst error=ERROR # /err files=glob.glob(entrada) for file in files: fichero=open(file,encoding='utf-8') try: for line in fichero: la=line.replace("-","") li=la.replace('”'+chr(10),'') li=li.split('"') line_DB(li) fichero.close() if TIME_RENAME=='True': execution=str(datetime.now()) execution=execution.replace('.','') execution=execution.replace('-','') execution=execution.replace(' ','_') execution_time=execution.replace(':','') base = os.path.splitext(file)[0] base=base+'.'+execution_time+'.txt' os.rename(file,base) file=base else: print('Hello') #This is where I need the code shutil.move(file, salida) con.commit() except: logging.error(sys.exc_info()) print(sys.exc_info()) fichero.close() shutil.move(file, error) 

Источник

Move and replace if same file name already exists?

If I execute same command and moving file which already existed in dst folder , I am getting shutil.Error: Destination path ‘./dstFolder/file.txt’ already exists . How to do move and replace if same file name already exists?

Читайте также:  Вывести все методы объекта python

2 Answers 2

If you specify the full path to the destination (not just the directory) then shutil.move will overwrite any existing file:

shutil.move(os.path.join(src, filename), os.path.join(dst, filename)) 

does this work as well on a network drive, i have a full path to the file but it’s not overriting, exiting with «File Exists»

@ecatmur I independently tested the same thing. 1. if the destination directory has the source filename already and you do shutil.move(src_filename, dst_dirname) an error is raised Error: Destination path ‘dst_dirname/src_filename’ already exists . 2. HOWEVER if you do dst_filename = os.path.join(dst_dirname, os.path.basename(src_filename)); shutil.move(src_filename, dst_filename) —> then you don’t get an exception raised.

You don’t need to specify the full path. If the filename is included in the destination path (relative or absolute) shutil will overwrite.

this looks a bit voodoo to me — why should there be different semantics for absolute and relative paths? this breaks the least surprise principle that python is so proudly following. I was expecting something like exist_ok=True , similar to what mkdir() does.

@TrevorBoydSmith, python3.6 reports the destination existing error, but it works fine wih Python 3.9.7.

Источник

How to copy directory recursively in python and overwrite all?

I’m trying to copy /home/myUser/dir1/ and all its contents (and their contents, etc.) to /home/myuser/dir2/ in python. Furthermore, I want the copy to overwrite everything in dir2/ . It looks like distutils.dir_util.copy_tree might be the right tool for the job, but not sure if there’s anything easier/more obvious to use for such a simple task. If it is the right tool, how do I use it? According to the docs there are 8 parameters that it takes. Do I have to pass all 8 are just src , dst and update , and if so, how (I’m brand new to Python). If there’s something out there that’s better, can someone give me an example and point me in the right direction?

Thanks @JoranBeasley (+1) — however, according to cp ‘s docs, the -f arg («force»): if an existing destination file cannot be opened, remove it and try again. this doesn’t seem to be the same as «overwrite all». Can you confirm it is the same and that whatever dir1 ‘s contents are all get (recrusively) copied to dir2 ‘s subtree? Thanks again!

@4herpsand7derpsago: cp overwrites files by default. There’s a switch that prevents it from overwriting files, but not the other way around.

7 Answers 7

Notice:

distutils has been deprecated and will be removed in Python 3.12. Consider looking for other answers at this question if you are looking for a post-3.12 solution.

Original answer:

You can use distutils.dir_util.copy_tree . It works just fine and you don’t have to pass every argument, only src and dst are mandatory.

However in your case you can’t use a similar tool like shutil.copytree because it behaves differently: as the destination directory must not exist this function can’t be used for overwriting its contents.

If you want to use the cp tool as suggested in the question comments beware that using the subprocess module is currently the recommended way for spawning new processes as you can see in the documentation of the os.system function.

Thanks! Just a note, I needed two imports to get this to work. See stackoverflow.com/questions/18908941/…

What is the logical/philosophical difference between shutil and distutils.dir_util ? Is one preferable to the other in general?

Also, beware that if you call this method twice with the same arguments it will fail if you cleaned the target directory: bugs.python.org/issue22132

Have a look at the shutil package, especially rmtree and copytree . You can check if a file / path exists with os.paths.exists() .

import shutil import os def copy_and_overwrite(from_path, to_path): if os.path.exists(to_path): shutil.rmtree(to_path) shutil.copytree(from_path, to_path) 

Vincent was right about copytree not working, if dirs already exist. So distutils is the nicer version. Below is a fixed version of shutil.copytree . It’s basically copied 1-1, except the first os.makedirs() put behind an if-else-construct:

import os from shutil import * def copytree(src, dst, symlinks=False, ignore=None): names = os.listdir(src) if ignore is not None: ignored_names = ignore(src, names) else: ignored_names = set() if not os.path.isdir(dst): # This one line does the trick os.makedirs(dst) errors = [] for name in names: if name in ignored_names: continue srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): copytree(srcname, dstname, symlinks, ignore) else: # Will raise a SpecialFileError for unsupported file types copy2(srcname, dstname) # catch the Error from the recursive copytree so that we can # continue with other files except Error, err: errors.extend(err.args[0]) except EnvironmentError, why: errors.append((srcname, dstname, str(why))) try: copystat(src, dst) except OSError, why: if WindowsError is not None and isinstance(why, WindowsError): # Copying file access times may fail on Windows pass else: errors.extend((src, dst, str(why))) if errors: raise Error, errors 

Источник

How to Move Files in Python: Single and Multiple File Examples

A quick way of moving a file from one place to another is using shutil.move() as shown:

We’ll look at some of the various options you’ve got for moving files around using Python. There’s also a quick example of how you could use the shutil and os libraries to tidy up your downloads folder in the first section. So if you’re someone that needs something like that in your life, then keep reading!

Option 1: shutil.move()

The example shown in the introduction uses the move() function from the shutil library. This function does what you’d expect and moves files from one location to the other, as follows:

shutil.move() works by first creating a copy of the file with the path defined by old_path and storing the copy in the new location, new_path . Finally, after the successful creation of the copy, Python deletes the original file located at old_path .

In cases where the original file is protected, shutil.move() will create a copy of the file in the new location, but Python will be unable to delete the original.

Most people have got quite messy download folders. So let’s look at a practical example of how we could use shutil.move() to store all the images in a download folder in a new folder called downloaded images :

Running this script inside a downloads folder will move any files with the extension .jpg or .JPG in the folder to the downloaded_images folder. Using os.listdir() returns a list of all the files in the folder. By then using os.mkdir(‘downloaded_images’) the downloaded_images folder is created. Using shutil.move() , Python can then move all the files in our images list to the new folder. This process is shown in the diagram below:

There’s a lot of room for improvement here. For example, we could upgrade our list comprehension to include more image types. We should also code in an if-else branch to see if the downloaded_images folder exists before running os.mkdir() . There’s also no reason we couldn’t expand this script to create separate folders for PDFs, executable files and anything else sitting in your downloads folder gathering dust!

Option 2: os.rename()

The os library also has a couple of options for moving files, one of these is os.rename() . os.rename() functions a little differently to shutil.move() .

Instead of copying the file in question, rename() alters the path of a file, which automatically changes the file location. See below for an example of how we could apply the function:

os.replace() works too. Despite the function being called replace() , it also moves files by renaming them. os.replace() can be implemented with an identical template to shutil.move() and os.rename() :

os.replace() and os.rename() can both be used to change a file or directory name. os.rename() reports errors differently depending on what operating system you’re running.

Whereas os.replace() will report errors uniformly across different systems, which may be the better choice when working on a program that needs compatibility with different operating systems.

Option 3: pathlib.Path().rename()

For a more object-oriented approach to moving files, pathlib is also an option.

By using the Path() function, Python creates a Path object. The rename() method then changes the path of the object, similarly to how os.rename() works:

We could also apply pathlib.Path().rename() to our script created earlier for moving images out of our downloads folder. See below for an example of this:

Below is a table that compares the speed difference of the three approaches:

Источник

Оцените статью