Solved

File Copying..

Posted on 2006-10-28
3
177 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now