Vasanthi Vuppuluri

Menu

  • About
  • Blog
  • Programming
  • Contact
  • About
  • Blog
  • Programming
  • Contact

April 10, 2015 / 0 comments

Recursively copy files and sub directories from one directory to another – Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python2
 
"""
@author: Vasanthi Vuppuluri
Date created: April 10, 2015
 
 
Purpose:
--------
- To rename a directory
- Copy content from another directory to the renamed another
 
 
Executing the script:
---------------------
python rename_copy_directories.py /Users/username/Desktop/directory_to_be_renamed new_name /Users/username/Desktop/copy_from
 
 
Output:
-------
- The original folder is renamed to what is specified and the files and sub-folders are recursively copied to the renamed folder
 
 
Sample input and output:
------------------------
- rename_directory_or_file()
Example directory (or) file path: /Users/username/Desktop/directory_to_be_renamed
new_name => The directory or file will be renamed to new_name
Example: new_name = hello
  Output:
/Users/username/Desktop/hello
 
- copy_contents()
shutil module cannot copy contents from source folder to destination folder if the \
destination folder or file is already present. Hence, we first recursively delete the\
folder to which we want the contents to be copied to.
It is ok to delete in this case, as we are essentially renaming an empty folder.
Contents are now copied to the newly renamed folder.
 
"""
 
import os
import sys
import errno
import shutil
 
def rename_directory_or_file(directory_path, new_name):
# Changing the path of the directory first
# We can do this by replacing the text after the last occurance of '/' in the path
# assuming the path is something similar to: /Users/username/Desktop/directory_to_be_renamed
# This method can rename both files and directories
renamed_directory_path = '/'.join(directory_path.split('/')[:-1]) + '/' + new_name
os.rename(directory_path, renamed_directory_path)
return renamed_directory_path
 
def copy_contents(source_directory, renamed_directory_path):
# Copies the files and folders from a specfied folder to the renamed folder
try:
# This copies directories only
shutil.copytree(source_directory, renamed_directory_path)
except OSError as e:
# Here we check if the error was a result of the source not being a directory
if e.errno == errno.ENOTDIR:
shutil.copy(source_directory, renamed_directory_path)
else:
print "Copy unsuccessfull!!"
print str(e)
 
def main():
directory_path = sys.argv[1] # Directory whose name is to be changed
new_name = sys.argv[2] # Name of the directory to which it has to be changed to
source_directory = sys.argv[3] # Path of the directory to copy the contents from
try:
renamed_directory_path = rename_directory_or_file(directory_path, new_name)
print "Renamed successfully!! \nNew path: %s" %(renamed_directory_path)
except Exception, e:
print str(e)
 
try:
shutil.rmtree(renamed_directory_path)
copy_contents(source_directory, renamed_directory_path)
print "Recursively copied the directory contents!!"
except Exception, e:
print str(e)
 
if __name__ == "__main__":
main()

About the author

vasanthi

Vasanthi Vuppuluri is a Computer Science graduate student at University of Houston with her research in the area of Natural Language Processing. She developed n-gram, collocation (with f-score of upto 100%) and multi-word expression extraction (with 100% precision) software. She is currently learning Hadoop and MapReduce concepts to be able to run large scale experiments to test the software developed. As part of her job at UofH, she also designs websites, maintains web server, administers SVN server, builds computer clusters.

Author archive

About the post

Programming, Python

Maximum sum sub-array – Kadane’s algorithm

Binary search on an ordered list – Python

Leave a Reply Cancel reply

Your email address will not be published.

Recent Posts

  • Range of IP addresses
  • Find the itinerary given a list of tickets
  • K-nearest neighbors: Max-heap implementation
  • Extract text from XML files
  • Maximum sum sub-array – Kadane’s algorithm

Recent Comments

  • sandeep on Installing Hadoop-2.5.1 on 64-bit Ubuntu-14.01
  • amir on Installing Hadoop-2.5.1 on 64-bit Ubuntu-14.01
  • vasanthi on Installing Hadoop-2.5.1 on 64-bit Ubuntu-14.01
  • Saumil on Installing Hadoop-2.5.1 on 64-bit Ubuntu-14.01
  • amir sanjar on Installing Hadoop-2.5.1 on 64-bit Ubuntu-14.01

Categories

  • drupal 7
  • Hadoop
  • Programming
  • Python
  • R
  • Ubuntu

© 2019 Vasanthi Vuppuluri

Theme by Anders Norén — Up ↑