473,574 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python timing

32 New Member
Would anyone know how to write a program in python that takes a directory (source) and backs up that directory into another directory (target directory)? I've been trying to work on it, but I have had no luck...any help?
Dec 23 '06 #1
37 5755
bvdet
2,851 Recognized Expert Moderator Specialist
Would anyone know how to write a program in python that takes a directory (source) and backs up that directory into another directory (target directory)? I've been trying to work on it, but I have had no luck...any help?
Python library modules 'os', 'os.path', and 'shutil' can be used to accomplish this task. This will get you on the way:
Expand|Select|Wrap|Line Numbers
  1. import os, shutil
  2.  
  3. def dir_list(dir_name):
  4.     fileList = []
  5.     for file in os.listdir(dir_name):
  6.         dirfile = os.path.join(dir_name, file)
  7.         if os.path.isfile(dirfile):
  8.             fileList.append(dirfile)
  9.     return fileList
  10.  
  11. if __name__ == '__main__':
  12.     dirNameS = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))
  13.     dirNameT = (os.path.join('C:\\', 'target_directory'))
  14.     fileList = dir_list(dirNameS)
  15.     for f in fileList:
  16.         print f, os.path.getsize(f), os.path.getmtime(f)
  17.  
  18.         # Untested
  19.         # You could test whether the file to be copied has a different size or a later date
  20.         # copy the file f to file or directory 'dirNameT'
  21.         # shutil.copy(f, dirNameT) 
  22.  
Dec 23 '06 #2
Soccerplayer1316
32 New Member
Python library modules 'os', 'os.path', and 'shutil' can be used to accomplish this task. This will get you on the way:
Expand|Select|Wrap|Line Numbers
  1. import os, shutil
  2.  
  3. def dir_list(dir_name):
  4.     fileList = []
  5.     for file in os.listdir(dir_name):
  6.         dirfile = os.path.join(dir_name, file)
  7.         if os.path.isfile(dirfile):
  8.             fileList.append(dirfile)
  9.     return fileList
  10.  
  11. if __name__ == '__main__':
  12.     dirNameS = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))
  13.     dirNameT = (os.path.join('C:\\', 'target_directory'))
  14.     fileList = dir_list(dirNameS)
  15.     for f in fileList:
  16.         print f, os.path.getsize(f), os.path.getmtime(f)
  17.  
  18.         # Untested
  19.         # You could test whether the file to be copied has a different size or a later date
  20.         # copy the file f to file or directory 'dirNameT'
  21.         # shutil.copy(f, dirNameT) 
  22.  

Thank you, but I'm new to python so where in this do i put the directory thats to be copied and where do i put the directory where its copied to?
Dec 23 '06 #3
bvdet
2,851 Recognized Expert Moderator Specialist
Thank you, but I'm new to python so where in this do i put the directory thats to be copied and where do i put the directory where its copied to?
Instead of the line:
Expand|Select|Wrap|Line Numbers
  1. print f, os.path.getsize(f), os.path.getmtime(f)
use something like this:
Expand|Select|Wrap|Line Numbers
  1. shutil.copy(f, dirNameT)
In the sample code, dirNameS is the source directory and dirNameT is the target directory.
Dec 23 '06 #4
Soccerplayer1316
32 New Member
Instead of the line:
Expand|Select|Wrap|Line Numbers
  1. print f, os.path.getsize(f), os.path.getmtime(f)
use something like this:
Expand|Select|Wrap|Line Numbers
  1. shutil.copy(f, dirNameT)
In the sample code, dirNameS is the source directory and dirNameT is the target directory.
ok so this is what I have:

Expand|Select|Wrap|Line Numbers
  1. import os, shutil
  2.  
  3. def dir_list(dir_name):
  4.     fileList = []
  5.     for file in os.listdir(dir_name):
  6.         dirfile = os.path.join(dir_name, file)
  7.         if os.path.isfile(dirfile):
  8.             fileList.append(dirfile)
  9.     return fileList
  10.  
  11. if __name__ == '__main__':
  12.     dirNameS = (os.path.join('C:\\drivers\\audio'))
  13.     dirNameT = (os.path.join('C:\\Drivers'))
  14.     fileList = dir_list(dirNameS)
  15.     for f in fileList:
  16.         shutil.copy(f, dirNameT)
Dec 23 '06 #5
Soccerplayer1316
32 New Member
ok so this is what I have:


import os, shutil

def dir_list(dir_na me):
fileList = []
for file in os.listdir(dir_ name):
dirfile = os.path.join(di r_name, file)
if os.path.isfile( dirfile):
fileList.append (dirfile)
return fileList

if __name__ == '__main__':
dirNameS = (os.path.join(' C:\\drivers\\au dio'))
dirNameT = (os.path.join(' C:\\Drivers'))
fileList = dir_list(dirNam eS)
for f in fileList:
shutil.copy(f, dirNameT)

sweet NVM I figured it out, I wasn't writing each specific folder after a comma, Thank you so much
Dec 23 '06 #6
bvdet
2,851 Recognized Expert Moderator Specialist
ok so this is what I have:


import os, shutil

def dir_list(dir_na me):
fileList = []
for file in os.listdir(dir_ name):
dirfile = os.path.join(di r_name, file)
if os.path.isfile( dirfile):
fileList.append (dirfile)
return fileList

if __name__ == '__main__':
dirNameS = (os.path.join(' C:\\drivers\\au dio'))
dirNameT = (os.path.join(' C:\\Drivers'))
fileList = dir_list(dirNam eS)
for f in fileList:
shutil.copy(f, dirNameT)
Indentation is an important part of Python. Without code tags the program structure is confuscated. See 'REPLY GUIDELINES' on this page.

You will not need 'os.path.join() ' if you supply the full path for dirNameS and dirNameT. For a backup program, you should test for the existance of the file on the target and provide the user a means to decide whether or not to overwrite it. The code should work for a simple copy utility.

Barton is right. If you are new to Python and learning to program, start simple and build on what you have learned a little at a time.
Dec 23 '06 #7
Soccerplayer1316
32 New Member
Indentation is an important part of Python. Without code tags the program structure is confuscated. See 'REPLY GUIDELINES' on this page.

You will not need 'os.path.join() ' if you supply the full path for dirNameS and dirNameT. For a backup program, you should test for the existance of the file on the target and provide the user a means to decide whether or not to overwrite it. The code should work for a simple copy utility.

Barton is right. If you are new to Python and learning to program, start simple and build on what you have learned a little at a time.

This is kind of python related, but would you know how I can find the directory name of a USB port such as when I hook up my digital camera it doesn't come out as I: drive as it does for flash drives and so on, how could I find the path name of the usb port, I am trying to create a back up of all the picture son my digital camera using this python backup program.
Dec 23 '06 #8
Soccerplayer1316
32 New Member
I am at the end of the program....What do I type into the program to have it run every 5 seconds?
Dec 23 '06 #9
bvdet
2,851 Recognized Expert Moderator Specialist
This is kind of python related, but would you know how I can find the directory name of a USB port such as when I hook up my digital camera it doesn't come out as I: drive as it does for flash drives and so on, how could I find the path name of the usb port, I am trying to create a back up of all the picture son my digital camera using this python backup program.
I do not know how to do that directly from the camera. Many card readers show up as drives, so it would be straightforward accessing the card that way. Maybe one of our experts can help. You might post this question to the Techie Talk forum.
Dec 23 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2356
by: Freddie | last post by:
Hi, I posted a while ago for some help with my word finder program, which is now quite a lot faster than I could manage. Thanks to all who helped :) This time, I've written a basic batch binary usenet poster in Python, but encoding the data into yEnc format is fairly slow. Is it possible to improve the routine any, WITHOUT using...
32
12448
by: dan | last post by:
It would be an understatement to say I love this language. What used to take me all day now takes 3 hours, and I can spend the rest of the time on my bike thinking about the problems from a high level instead of wrestling with arcane compiler problems, etc. Back in the day, when looking at an interpreted language (or even compiled ones)...
15
2494
by: Guyon Morée | last post by:
Hi all, I am working on a Huffman encoding exercise, but it is kinda slow. This is not a big problem, I do this to educate myself :) So I started profiling the code and the slowdown was actually taking place at places where I didn't expect it. after I have created a lookup-table-dictionary with encodings like {'d':'0110', 'e':'01' etc}...
24
3418
by: Richard Blackwood | last post by:
Is it possible to prototype an operating system in Python? If so, what would such a task entail? (i.e. How would one write a boot-loader in Python?) - Richard B.
6
2244
by: Gonzalo Monzón | last post by:
Hi all! I have been translating some Python custom C extension code into Python, as I need these modules to be portable and run on a PocketPC without the need of compile (for the purpose its a must 2.4 as it is the last PythonCE release with great improvements). But I've been stuck with a script wich does not work as expected once...
2
2544
by: Oeyvind Brandtsegg | last post by:
hello I'm writing a Python application for live music performance/improivsation, using csound as the synthesis engine, and Python for compositional algorithms, GUI and control. I creating several threads: one for GUI one for csound, one for a timed queue (timed automation events),
14
2658
by: Hendrik van Rooyen | last post by:
Hi, I get the following: hvr@LINUXBOXMicrocorp:~/Controller/libpython display.py UpdateStringProc should not be invoked for type font Aborted and I am back at the bash prompt - this is most frustrating, as there is no friendly traceback to help me guess where its coming from.
0
1184
by: Cameron Laird | last post by:
QOTW: "I have a fake supervisor reference generator for job interviews, a fake house inspection generator for real estate transactions, and a fake parole testimony generator - maybe you could adapt one of them (unfortunately, they are written in dissembler)." - Paul McGuire "... I think that is the most interesting work in CS in many...
7
2417
by: Protocol | last post by:
Hello all Is Python suitable for building a multi-track midi sequencer (with a gui), that would run on windows / mac ? I fail to find sufficient information on this, being a newbie and all. Furthermore, i found references on Python not being really able of multi-threading, that further adds to the confusion. Please assist.
0
7799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8042
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8226
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7806
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5612
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5297
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3739
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.