Is there a way, in python, I can copy a file in a directory and duplicate it in the same directory with a different name? - TagMerge
4Is there a way, in python, I can copy a file in a directory and duplicate it in the same directory with a different name?Is there a way, in python, I can copy a file in a directory and duplicate it in the same directory with a different name?

Is there a way, in python, I can copy a file in a directory and duplicate it in the same directory with a different name?

Asked 10 months ago
0
4 answers

shutil.copy() can do that if you specify the destination filename as well as destination folder:

import shutil
shutil.copy(r'c:\temp\file1.txt', r'c:\temp\file2.txt')

Source: link

0

Adding on to TessellatingHeckler's answer, if you have a file with an arbitrary directory, you can use os.path.dirname and os.path.join to create a new filename in the same directory:

import os
import shutil

original = r'c:\temp\file1.txt'
original_dir = os.path.dirname(original)          # r'c:\temp'
new_name = 'file2.txt'
new_path = os.path.join(original_dir, new_name)   # r'c:\temp\file2.txt'

shutil.copy(original, new_path)

You could of course do that more compactly if you wish. You'll also notice from the shutil.copy() docs that shutil features multiple different copying methods, each one with pros and cons. For example, shutil.copy2() attempts to preserve metadata. It's up to you to decide which method is best for your situation.

Source: link

0

import shutil
src_file_obj=open('src.txt', 'rb')
targ_file_obj= open('targ.txt' , 'wb')
shutil.copyfileobj( src_file_obj , targ_file_obj )
The copyfile() method copies the content from the source to the target file using the file paths. It returns the target file path. The target file path must be writeable or else an OSerror exception would occur.
import shutil
shutil.copyfile( 'src.txt' , 'targ.txt' )
import shutil
shutil.copy('/Users/test/file.txt', '/Users/target/')
The copy2() method is used exactly the same way as of the copy() method. They also function in the same way, except for the fact that copy2() also copies the meta-data from the source file.
import shutil
shutil.copy2('/Users/test/file.txt', '/Users/target/')
The popen() method creates a pipe to the command, cmd. The method returns a file object connected to the cmd pipe. Take a look at the code below,
#for Windows
import os
os.popen('copy src.txt targ.txt' )

Source: link

0

Let’s take a look at how we can use the shutil.copyfile() method to copy a file using Python:
# Copy a file to a destination with shutil.copyfile()

import shutil
shutil.copyfile('/Users/datagy/Desktop/file.py', '/Users/datagy/Desktop/file2.py')
Let’s take a look at how we can copy a file to a directory using shutil.copy():
# Copy a file to a destination with shutil.copy()

import shutil
shutil.copy('/Users/datagy/Desktop/file.py', '/Users/datagy/Desktop/Folder/')
Let’s see how we can do this:
# Copy a file to a destination with shutil.copy2() with metadata

import shutil
shutil.copy('/Users/datagy/Desktop/file.py', '/Users/datagy/Desktop/Folder/')
Let’s see how we can do this:
# Copy a file to a destination with shutil.copyfileobj()

import shutil

source_file_path = '/Users/nikpi/Desktop/file.py'
source_file = open(source_file_path, 'rb')
destination_file_path = '/Users/nikpi/Desktop/file.py'
destination_file = open(source_file_path, 'wb')

shutil.copyfileobj(source_file, destination_file)

Source: link

Recent Questions on python

    Programming Languages