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?
37 5739 bvdet 2,851
Expert Mod 2GB
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: - import os, shutil
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
dirNameS = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))
-
dirNameT = (os.path.join('C:\\', 'target_directory'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
print f, os.path.getsize(f), os.path.getmtime(f)
-
-
# Untested
-
# You could test whether the file to be copied has a different size or a later date
-
# copy the file f to file or directory 'dirNameT'
-
# shutil.copy(f, dirNameT)
-
Python library modules 'os', 'os.path', and 'shutil' can be used to accomplish this task. This will get you on the way: - import os, shutil
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
dirNameS = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))
-
dirNameT = (os.path.join('C:\\', 'target_directory'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
print f, os.path.getsize(f), os.path.getmtime(f)
-
-
# Untested
-
# You could test whether the file to be copied has a different size or a later date
-
# copy the file f to file or directory 'dirNameT'
-
# shutil.copy(f, dirNameT)
-
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?
bvdet 2,851
Expert Mod 2GB
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: - print f, os.path.getsize(f), os.path.getmtime(f)
use something like this:
In the sample code, dirNameS is the source directory and dirNameT is the target directory.
Instead of the line: - print f, os.path.getsize(f), os.path.getmtime(f)
use something like this:
In the sample code, dirNameS is the source directory and dirNameT is the target directory.
ok so this is what I have: -
import os, shutil
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
dirNameS = (os.path.join('C:\\drivers\\audio'))
-
dirNameT = (os.path.join('C:\\Drivers'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
ok so this is what I have:
import os, shutil
def dir_list(dir_name):
fileList = []
for file in os.listdir(dir_name):
dirfile = os.path.join(dir_name, file)
if os.path.isfile(dirfile):
fileList.append(dirfile)
return fileList
if __name__ == '__main__':
dirNameS = (os.path.join('C:\\drivers\\audio'))
dirNameT = (os.path.join('C:\\Drivers'))
fileList = dir_list(dirNameS)
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
bvdet 2,851
Expert Mod 2GB
ok so this is what I have:
import os, shutil
def dir_list(dir_name):
fileList = []
for file in os.listdir(dir_name):
dirfile = os.path.join(dir_name, file)
if os.path.isfile(dirfile):
fileList.append(dirfile)
return fileList
if __name__ == '__main__':
dirNameS = (os.path.join('C:\\drivers\\audio'))
dirNameT = (os.path.join('C:\\Drivers'))
fileList = dir_list(dirNameS)
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.
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.
I am at the end of the program....What do I type into the program to have it run every 5 seconds?
bvdet 2,851
Expert Mod 2GB
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.
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.
Yea I figured it out, you can't do the camera, it just won't show up as a mass storage device so I just plug the memory card in, but would you by any chance know how to make the program run every 5 seconds? Thats all I need and the program will be finished.
I am at the end of the program....What do I type into the program to have it run every 5 seconds?
Please read "Posting Guidelines" to learn what CODE tags look like. Post your program (try to use CODE tags). I'll show you how to run your program ever 5 seconds.
Please read "Posting Guidelines" to learn what CODE tags look like. Post your program (try to use CODE tags). I'll show you how to run your program ever 5 seconds.
thank you, yea I have no clue on how to do the code thing I keep on trying but I'll try again now, ok so here it is with help from the other thread this is the code. - import os, shutil
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
dirNameS = (os.path.join('E:\\', 'DCIM', '140canon'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
thank you, yea I have no clue on how to do the code thing I keep on trying but I'll try again now, ok so here it is with help from the other thread this is the code. - import os, shutil
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
dirNameS = (os.path.join('E:\\', 'DCIM', '140canon'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
Well, you did it! Great job! - import os, shutil
-
from time import time
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
def CopyFiles():
-
dirNameS = (os.path.join('E:\\', 'DCIM', '140canon'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
duration = 30 # run for 30 seconds total
-
hasrunfor = 0 # count how long we have run
-
while hasrunfor < duration:
-
endTime = time() + 5 # time() counts in seconds
-
CopyFiles()
-
while time() < endTime:
-
pass
-
hasrunfor += 5
This will run for 30 seconds total trying to copy files every 5 seconds. I expect that you may get a file error before the 30 seconds are up (especially if you unplug your memory stick). There are ways to make python ignore errors, but this is very advanced programming.
Well, you did it! Great job! - import os, shutil
-
from time import time
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
def CopyFiles():
-
dirNameS = (os.path.join('E:\\', 'DCIM', '140canon'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
duration = 30 # run for 30 seconds total
-
hasrunfor = 0 # count how long we have run
-
while hasrunfor < duration:
-
endTime = time() + 5 # time() counts in seconds
-
CopyFiles()
-
while time() < endTime:
-
pass
-
hasrunfor += 5
This will run for 30 seconds total trying to copy files every 5 seconds. I expect that you may get a file error before the 30 seconds are up (especially if you unplug your memory stick). There are ways to make python ignore errors, but this is very advanced programming.
Thank you so much, ouch so there comes a error? I was thinking about having it run all the time and even if there isn't the memory stick in it, but once there is for it to work, I'm going to test it out right now to see if it will work...Thank you for all of yourhelp, this is the best python forum...I will tell you what results I got
ok so everything runs smooth, but the program won't even run when the directory is not there( when the memory stick is taken out), which kind of defeats the purpose of having it run even if the directory isn't there and not showing errors, but extracting the files when the directory is there....even though it is advance programming would you know how I could get python to ignore it even if the memory stick is plugged out but to get the files once the memory stick is in? Could this be solved by just adding another source from where to copy files such as a hard drive would that ignore the fact that one of the drives to be copied is plugged out?
ok so everything runs smooth, but the program won't even run when the directory is not there( when the memory stick is taken out), which kind of defeats the purpose of having it run even if the directory isn't there and not showing errors, but extracting the files when the directory is there....even though it is advance programming would you know how I could get python to ignore it even if the memory stick is plugged out but to get the files once the memory stick is in? Could this be solved by just adding another source from where to copy files such as a hard drive would that ignore the fact that one of the drives to be copied is plugged out?
Please post the error that you are getting.
bvdet 2,851
Expert Mod 2GB
Yea I figured it out, you can't do the camera, it just won't show up as a mass storage device so I just plug the memory card in, but would you by any chance know how to make the program run every 5 seconds? Thats all I need and the program will be finished.
- from time import sleep
-
while True:
-
try:
-
for f in os.listdir('D:\\'):
-
print f
-
sleep(5)
-
except:
-
break
Replace "D:\\" with the path to the media. Replace 'print f' with whatever you want to do to each file. This script will run until the media is removed or ejected.
Please post the error that you are getting.
Oh no it doesn't give me a error, what happens is I converted it to a .exe file when the flash drive is in, it runs smooth and the command window for that file is open, when i take out the flash drive and click on the exe the program turns on and the window closes automatically now to get the error you are asking for I'll take the flash drive out and run it in PythonWin, and this is the error
fileList = dir_list(dirNameS)
File "C:\Documents and Settings\Robert\Desktop\Python Files\backuptry.py", line 6, in dir_list
for file in os.listdir(dir_name):
WindowsError: [Error 2] The system cannot find the path specified: 'I:\\SD/*.*'
- from time import sleep
-
while True:
-
try:
-
for f in os.listdir('D:\\'):
-
print f
-
sleep(5)
-
except:
-
break
Replace "D:\\" with the path to the media. Replace 'print f' with whatever you want to do to each file. This script will run until the media is removed or ejected.
Yea but I need something that will ignore the errors and ignore if the media was ejected and keep working and once the media is plugged back in to keep on backing up
bvdet 2,851
Expert Mod 2GB
Yea but I need something that will ignore the errors and ignore if the media was ejected and keep working and once the media is plugged back in to keep on backing up
This will run forever. It's not a good thing to do though. -
from time import sleep
-
while True:
-
try:
-
for f in os.listdir('D:\\'):
-
print f
-
sleep(5)
-
except:
-
pass
This will run forever. It's not a good thing to do though. -
from time import sleep
-
while True:
-
try:
-
for f in os.listdir('D:\\'):
-
print f
-
sleep(5)
-
except:
-
pass
But will it ignore the error if the drive is taken out? because thats all that matters, and I shut it off by just exiting out of the exe that I make out of the python file?
bvdet 2,851
Expert Mod 2GB
thank you, yea I have no clue on how to do the code thing I keep on trying but I'll try again now, ok so here it is with help from the other thread this is the code. - import os, shutil
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
dirNameS = (os.path.join('E:\\', 'DCIM', '140canon'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
Barton - Maybe you should combine these threads. Is it possible?
bvdet 2,851
Expert Mod 2GB
But will it ignore the error if the drive is taken out? because thats all that matters, and I shut it off by just exiting out of the exe that I make out of the python file?
The try statement executes the code that follows. When an error occurs, execution jumps to the except statement. 'pass' does just what it says: pass - no matter what error is encountered.
bvdet 2,851
Expert Mod 2GB - from time import sleep
-
while True:
-
try:
-
for f in os.listdir('D:\\'):
-
print f
-
sleep(5)
-
except:
-
sleep(5)
-
pass
This is better. At least the script pauses 5 seconds no matter what.
- from time import sleep
-
while True:
-
try:
-
for f in os.listdir('D:\\'):
-
print f
-
sleep(5)
-
except:
-
sleep(5)
-
pass
This is better. At least the script pauses 5 seconds no matter what.
I am about to try this code but the other one, it didn't even copy the files, with sleep are you turning off the file because I'm trying to get it to execute every 5 seconds
no wonder the last one didn't work, I forgot to change the D:\\ =[
This will run forever. It's not a good thing to do though. -
from time import sleep
-
while True:
-
try:
-
for f in os.listdir('D:\\'):
-
print f
-
sleep(5)
-
except:
-
pass
A little knowledge can be a dangerous thing.
ok so if this is the code - import os, shutil
-
from time import time
-
-
def dir_list(dir_name):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
return fileList
-
-
if __name__ == '__main__':
-
def CopyFiles():
-
dirNameS = (os.path.join('I:\\', 'SD'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
duration = 60 # run for 30 seconds total
-
hasrunfor = 0 # count how long we have run
-
while hasrunfor < duration:
-
endTime = time() + 5 # time() counts in seconds
-
CopyFiles()
-
while time() < endTime:
-
pass
-
hasrunfor += 5
-
what do I add to this to ignore the errors when the flash drive is taken out?...And what do I add to keep the program running after passing the error...and once the flash drive is added in and its still running will it do the back up that it was made to do?
The try statement executes the code that follows. When an error occurs, execution jumps to the except statement. 'pass' does just what it says: pass - no matter what error is encountered.
I'll give him this one... - import os, shutil
-
from time import time
-
-
def dir_list(dir_name):
-
fileList = []
-
try:
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
except WindowsError:
-
pass
-
return fileList
-
-
if __name__ == '__main__':
-
def CopyFiles():
-
dirNameS = (os.path.join('E:\\', 'DCIM', '140canon'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
-
duration = 30 # run for 30 seconds total
-
hasrunfor = 0 # count how long we have run
-
while hasrunfor < duration:
-
endTime = time() + 5 # time() counts in seconds
-
CopyFiles()
-
while time() < endTime:
-
pass
-
hasrunfor += 5
Barton - Maybe you should combine these threads. Is it possible?
Ok, guys. I'm going to merge these threads. I'll stick with "Python timing" as the title.
ok can I just say...the code doesn't work once the WindowsError: pass thing was added, it doesn't work...when i run it I get a syntax error and when i try to convert it to a exe it won't even do it because the something is wrong with the code
ok can I just say...the code doesn't work once the WindowsError: pass thing was added, it doesn't work...when i run it I get a syntax error and when i try to convert it to a exe it won't even do it because the something is wrong with the code
Sorry. Had bad indentation on one line. Here's the fix: - import os, shutil
-
from time import time
-
-
def dir_list(dir_name):
-
fileList = []
-
try:
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
fileList.append(dirfile)
-
except WindowsError:
-
pass
-
return fileList
-
-
if __name__ == '__main__':
-
def CopyFiles():
-
dirNameS = (os.path.join('E:\\', 'DCIM', '140canon'))
-
dirNameT = (os.path.join('C:\\', 'Camera'))
-
fileList = dir_list(dirNameS)
-
for f in fileList:
-
shutil.copy(f, dirNameT)
-
-
duration = 30 # run for 30 seconds total
-
hasrunfor = 0 # count how long we have run
-
while hasrunfor < duration:
-
endTime = time() + 5 # time() counts in seconds
-
CopyFiles()
-
while time() < endTime:
-
pass
-
hasrunfor += 5
THANK YOU FOR ALL OF THE HELP BOTH OF YOU!!!! it finally works it does exactly what I need it to do.
THANK YOU FOR ALL OF THE HELP BOTH OF YOU!!!! it finally works it does exactly what I need it to do.
You are VERY welcome. Be sure to start a new discussion for you next question. Thanks and keep posting,
Barton
I'll be sure to ask fro help on this forum, you guys are really good at helping, other forums, rn't too good....KUDOS
bvdet 2,851
Expert Mod 2GB
I'll be sure to ask fro help on this forum, you guys are really good at helping, other forums, rn't too good....KUDOS
I am glad to help. Good work Barton!
I am glad to help. Good work Barton!
You too. Thanks BV.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
| |