Solved

File Copying..

Posted on 2006-10-28
3
178 Views
Last Modified: 2010-04-16
I am pretty new to Python. Can someone tell me/good tutorial how to make file copies in Python..
0
Comment
Question by:templeavenue
3 Comments
 

Expert Comment

by:zenpro
ID: 17826403
Hi templeavenue,

you can use the following:

import shutil
shutil.copy2(source, destination)


shutil.copy will perform the copy using the current date and time, while copy2 will copy over also the date and time of the original file's last modification. You can read more about Shutil here: http://docs.python.org/lib/module-shutil.html

0
 

Author Comment

by:templeavenue
ID: 17827497
sorry... actually..what I wanted to do is read a drive (hard dirve, cd) without knowing the name/structures of the file/folders(if possible)... sorry for confusion... can i still use those functions??

Thanks,
Kelvin
0
 
LVL 28

Accepted Solution

by:
pepr earned 50 total points
ID: 17836428
The os module may be your friend. Try this to list all the directories and files from the root. Have a look at the walk() function in the stadard Python documentation. You can process the retrieved information in the way you need and possibly to combine it with the above mentioned shutil module.

import os

top = '/'
for dirpath, dirnames, filenames in os.walk(top):
    print dirpath

    for d in dirnames:
        print '  D  ' +  os.path.join(dirpath, d)

    for f in filenames:
        print '  F  ' +  os.path.join(dirpath, f)

    print



0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question