Re: [Tutor] copy files selectively from source to destination
anatta anatta wrote: > Here is my working code - to copy files from one drive to another. > > I however want to copy selective files. > > For example I like to copy only .txt files only from the source to > destination, and not other types of files. > > How could I do this selective copying? Such a question almost answers itself when you put a bit more structure into your code. You might write a generator that produces (sourcefile, destfile) pairs and a copyfile() function that performs the same checks you have inlined in your code below. A sketch of the resulting stript: def filepairs(sourcefolder, destfolder): for root, dirs, files in os.walk(sourcefolder): for name in files: sourcefile = ... destfile = ... yield sourcefile, destfile def copyfile(sourcefile, destfile): if not os.path.isfile(destfile): ... # copy else: ... # complain for sourcefile, destfile in filepairs("H://", "O://test_o"): copyfile(sourcefile, destfile) To copy only select files you have to add a check to the for loop: for sourcefile, destfile in filepairs(...): if copy_wanted(sourcefile): copyfile(sourcefile, destfile) Now you can experiment with various implementations of that function without touching the bulk of your code. To copy only text files it might look like this def copy_wanted(sourcefile): return os.path.splitext(sourcefile)[0] == ".txt" ... or this def predicate_from_glob(glob): def is_match(filename): return fnmatch.fnmatch(filename, glob) return is_match copy_wanted = predicate_from_glob("*.txt") ... or something completely different like, say, a check based on the MIME type. > > Thanks in advance for the hints. > > > Best, > > Kumar. > > > # -*- coding: utf-8 -*- > """ > Created on Wed Jun 01 17:05:07 2016 > > @author: anatta > """ > > import os > import shutil > sourcePath = r'H://' > destPath = r'O://test_o/' > ls=os.listdir('.')#list current dir > #print('listing current dir\n') > #print(ls) > for root, dirs, files in os.walk(sourcePath): > > #figure out where we're going > dest = destPath + root.replace(sourcePath, '') > > #if we're in a directory that doesn't exist in the destination folder > #then create a new folder > if not os.path.isdir(dest): > os.mkdir(dest) > print 'Directory created at: ' + dest > > #loop through all files in the directory > for f in files: > > #compute current (old) & new file locations > oldLoc = root + '\\' + f > newLoc = dest + '\\' + f > > if not os.path.isfile(newLoc): > try: > shutil.copy2(oldLoc, newLoc) > print 'File ' + f + ' copied.' > except IOError: > print 'file "' + f + '" already exists' > > > ___ > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] copy files selectively from source to destination
On 12/05/2016 08:20 AM, anatta anatta wrote: Dear tutor! Here is my working code - to copy files from one drive to another. I however want to copy selective files. For example I like to copy only .txt files only from the source to destination, and not other types of files. How could I do this selective copying? You could test f as you loop over the files to ensure it ends '.txt'. Emile Thanks in advance for the hints. Best, Kumar. # -*- coding: utf-8 -*- """ Created on Wed Jun 01 17:05:07 2016 @author: anatta """ import os import shutil sourcePath = r'H://' destPath = r'O://test_o/' ls=os.listdir('.')#list current dir #print('listing current dir\n') #print(ls) for root, dirs, files in os.walk(sourcePath): #figure out where we're going dest = destPath + root.replace(sourcePath, '') #if we're in a directory that doesn't exist in the destination folder #then create a new folder if not os.path.isdir(dest): os.mkdir(dest) print 'Directory created at: ' + dest #loop through all files in the directory for f in files: #compute current (old) & new file locations oldLoc = root + '\\' + f newLoc = dest + '\\' + f if not os.path.isfile(newLoc): try: shutil.copy2(oldLoc, newLoc) print 'File ' + f + ' copied.' except IOError: print 'file "' + f + '" already exists' ___ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] copy files selectively from source to destination
On 05/12/16 16:20, anatta anatta wrote: > I however want to copy selective files. > For example I like to copy only .txt files only > How could I do this selective copying? By applying an if test just before you copy filetype = '.txt' # or read it as an input ... if sourcefile extension == filetype copy sourcefile to destination You can extract the extension using the os.path module functions such as splitext(p) Split the extension from a pathname. Extension is everything from the last dot to the end, ignoring leading dots. Returns "(root, ext)"; ext may be empty. (END) > if not os.path.isfile(newLoc): > try: The if test goes right about here... or you could even put it higher up before all your tests. > shutil.copy2(oldLoc, newLoc) > print 'File ' + f + ' copied.' > except IOError: > print 'file "' + f + '" already exists' An alternative option is to use glob.glob on the current folder to get a list of files that match your desired pattern and copy only those files. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] copy files selectively from source to destination
Dear tutor! Here is my working code - to copy files from one drive to another. I however want to copy selective files. For example I like to copy only .txt files only from the source to destination, and not other types of files. How could I do this selective copying? Thanks in advance for the hints. Best, Kumar. # -*- coding: utf-8 -*- """ Created on Wed Jun 01 17:05:07 2016 @author: anatta """ import os import shutil sourcePath = r'H://' destPath = r'O://test_o/' ls=os.listdir('.')#list current dir #print('listing current dir\n') #print(ls) for root, dirs, files in os.walk(sourcePath): #figure out where we're going dest = destPath + root.replace(sourcePath, '') #if we're in a directory that doesn't exist in the destination folder #then create a new folder if not os.path.isdir(dest): os.mkdir(dest) print 'Directory created at: ' + dest #loop through all files in the directory for f in files: #compute current (old) & new file locations oldLoc = root + '\\' + f newLoc = dest + '\\' + f if not os.path.isfile(newLoc): try: shutil.copy2(oldLoc, newLoc) print 'File ' + f + ' copied.' except IOError: print 'file "' + f + '" already exists' ___ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] copy files + directory tree via ftp
Hello I have a problem when copy files and directory tree in ftp, scripts in here only copy one file: from ftplib import FTP rootList = [] session = FTP() session.connect('workstation', port=21) session.login(user='saiki', passwd='saiki') session.retrlines('LIST', rootList.append) f=open('D:/PARAMS.LST','rb') session.storbinary('STOR '+af, f, 1024) f.close() session.close() f anyone has any ideas I would appreciate it. Thank you Nelson Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos more. http://mobile.yahoo.com/go?refer=1GNXIC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] copy files + directory tree via ftp
Nelson Kusuma wrote: Hello I have a problem when copy files and directory tree in ftp, scripts in here only copy one file: To transfer an entire directory with ftplib you will have to read the local directory with os.listdir() and loop to send the files. There are a few higher-level modules written on top of ftplib that might be helpful directly or as examples: http://pypi.python.org/pypi/ftputil/2.2.3 (site is down at the moment) http://www.nedbatchelder.com/code/modules/ftpupload.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copy files
Alan,Thanks for that. I had the wrong file names, now it works, in case anyone is interested here is the code. I use it because at work I need to change different versions of sqlnet.ora :-def compare_files(file_name1, file_name2): x = filecmp.cmp (file_name1, file_name2) print x return xdef change_files(file_1, file_2, cwd): yesorno = raw_input("Do you want them to be the same Y or N ") #file_1 = cwd+file_1 #file_2 = cwd+file_2 yesorno = string.upper(yesorno) if yesorno == 'Y': try: os.remove(file_2) print "removed file 2" except: print "could not remove file" try: shutil.copy(file_1, file_2) print "the copy part thinks it worked" except: print "it did not work" else: print "ok not changing anything" def did_it_work(file_1, file_2): #this is to debug the copy,only run if you are having problems afterchange = compare_files(file_1,file_2 ) if afterchange == True: &! nbsp; print "the copy went fine" else: print "problem with the copy" + str(afterchange) #main import shutil import string import filecmp import os a = os.getcwd() print a file1 = "SQLNETpersonal.ORA" file2 = "SQLNETclients.ORA" file3 = "SQLNET.ORA" x = compare_files(file1,file3 ) if x == False: print file1 + " and "+ file3+ " are different" #print "test1 and test3 are different" change_files(file1, file3,a) #did_it_work(file1, file3) else: x = compare_files(file2,file3 ) if x == False: print file2 + " and " +file3 +" are different" change_files(file2, file3,a) ! sp; #did_it_work(file2, file3) print "program finished"Alan Gauld [EMAIL PROTECTED] wrote:The only problem is that despite the fact that as the same user, I can manually change these files (so I must have the right file permissions ?)Can you do it from within python at the promptUse os.getcwd() to find out where you areUse os.chdir() to navigateUse os.listdir() to list the folder contentsand use shutil.copy() to copy the files.Doing it manually from inside the prompt shouldshow up any problems. You can even try the filecompcall from in there too... is a powerful tool.HTH,Alan GAuthor of the learn to program web tutorhttp://www.freenetpages.co.uk/hp/alan.gauld-- "Then you will know the truth, and the truth will set you free." John 8:32 " As Pastor Niemöller said, first they came for Piglet and I did not speak out because I was not a Disney character." http://www.telegraph.co.uk/opinion/main.jhtml?xml=/opinion/2005/10/04/do0402.xml "When the facts change, I change my opinions, what do you do sir ?" John Keynes. To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copy files
Alan,Thanks for that. Hopefully this now is easier to read. The only problem is that despite the fact that as the same user, I can manually change these files (so I must have the right file permissions ?) - the copying does not happening.def compare_files(file_name1, file_name2): x = filecmp.cmp (file_name1, file_name2) print x return xdef change_files(file_1, file_2): yesorno = raw_input("Do you want them to be the same Y or N ") yesorno = string.upper(yesorno) if yesorno == 'Y': try: shutil.copy(file_1, file_2) print "the copy part thinks it worked" except: print "it did not work" def did_it_work(file_1, file_2): #this is to debug the copy afterchange = compare_files("test1.txt","test3.txt" ) if afterchange == 'True': print "the copy went fine" else: print "problem with the copy" #main import shutil import string import filecmpx = compare_files("test1.txt","test3.txt" ) if x == False: print "test1 and test3 are different" change_files("test1.txt", "text3.txt") did_it_work("test1.txt", "text3.txt") else: x = compare_files("test2.txt","test3.txt" ) i! f x == False: print "test2 and test3 are different" change_files("test2.txt", "text3.txt") did_it_work("test2.txt", "text3.txt") print "program finished" Alan Gauld [EMAIL PROTECTED] wrote: David,Can I suggest you rethink your variable names?That might make it clearer what is happening. def compare_files(file_name1, file_name2): x = cmp (file_name1, file_name2) return xSeems fair enough except file_name1 and file_name2 are actually file *contents*!And in fact the function really just compares 2 strings. in fact it compares 2 anythings,It just returns the resulkt of cmp() so you could lose it entirely!
You might find it easier to use the filecmp.cmp() function which comparestwo files using their filenames as arguments. def open_files(file_name): file_is = open(file_name,'r') file_conts = file_is.read() return file_contsAnd "open_files" actually only opens one file and then returns it contents.So a name like "contents_of(filename)" would be more descriptive def change_files(file_1, file_2): yesorno = raw_input("Do you want them to be the same Y or N ") yesorno = string.upper(yesorno) if yesorno == 'Y': try: shutil.copy2(file_1, file_2) print "copy has been done " except: print "it did not work"The user prompt doesn't give the user much clue what he is agreeing tomake the same but otherwise this seems OK. I assume you have writeaccess to both files? #main file_name1 = open_files("test1.txt") file_name2 = open_files("test2.txt") file_name3 = open_files("test3.txt")the 3 variables above each contain not the *name* of the file but the contents. import shutil import string x = compare_files(file_name3, file_name1)x = cmp(f1,f2)would be just as useful. if x != 0: print "test1 and test3 are different" change_files("test1.txt", "text3.txt", file_name1)The function above has 2 parameters but you are passing 3 arguments.I'd expect Python to complain at that? Also the first two are filenamesbut the last is the contents of file1 else: file_name1 = "test2.txt"but here you overwrite the contents with the filename print file_name1and print "text2.txt"I'm not sure what thats trying to do? ! x = compare_files(file_name3, file_name2)again a simple cmp() would do the same job if x != 0: print "test2 and test3 are different" change_files("test2.txt", "text3.txt")Since you didn't mention an error I assume your program alwaysgoes down this path?Despite the confusing names it looks like the program must begoing down the second branch and trying to copy the two files.The only obvious thing I can think of is that the files do not havewrite permissions set appropriately?Not sure if that helps at all but the best I can do at 12:45am...:-)Alan G. To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copy files
The only problem is that despite the fact that as the same user, I can manually change these files (so I must have the right file permissions ?) Can you do it from within python at the prompt Use os.getcwd() to find out where you are Use os.chdir() to navigate Use os.listdir() to list the folder contents and use shutil.copy() to copy the files. Doing it manually from inside the prompt should show up any problems. You can even try the filecomp call from in there too... is a powerful tool. HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Copy files
I wrote a small program that compares files test1 and test2 to test3. If the files are different then it copies either test1 to test3 or the reverse.The problem is that the although the shutil.copy2 in thefunction change_files works without error it does not copy. Can anyone see what is wrong :-def compare_files(file_name1, file_name2): x = cmp (file_name1, file_name2) return xdef open_files(file_name): file_is = open(file_name,'r') file_conts = file_is.read() return file_contsdef change_files(file_1, file_2): yesorno = raw_input("Do you want them to be the same Y or N ") yesorno = string.upper(yesorno) if yesorno == 'Y': try: shutil.copy2(file_1, file_2) print "copy has been done " except: print "it did not work" #main file_name1 = open_files("test1.txt") file_name2 = open_files("test2.txt") file_name3 = open_files("test3.txt") import shutil import stringx = compare_files(file_name3, file_name1) if x != 0: print "test1 and test3 are different" change_files("test1.txt", "text3.txt", file_name1) else: file_name1 = "test2.txt" print file_name1 x = compare_files(file_name3, file_name2) # print "x is " + str(x) if x != 0: print "test2 and test3 are different" change_files("test2.txt", "text3.txt") print "program finished"-- "Then you will know the truth, and the truth will set you free." John 8:32 " As Pastor Niemöller said, first they came for Piglet and I did not speak out because I was not a Disney character." http://www.telegraph.co.uk/opinion/main.jhtml?xml=/opinion/2005/10/04/do0402.xml "When the facts change, I change my opinions, what do you do sir ?" John Keynes. To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copy files
David, Can I suggest you rethink your variable names? That might make it clearer what is happening. def compare_files(file_name1, file_name2): x = cmp (file_name1, file_name2) return x Seems fair enough except file_name1 and file_name2 are actually file *contents*! And in fact the function really just compares 2 strings. in fact it compares 2 anythings, It just returns the resulkt of cmp() so you could lose it entirely! You might find it easier to use the filecmp.cmp() function which compares two files using their filenames as arguments. def open_files(file_name): file_is = open(file_name,'r') file_conts = file_is.read() return file_conts And open_files actually only opens one file and then returns it contents. So a name like contents_of(filename) would be more descriptive def change_files(file_1, file_2): yesorno = raw_input(Do you want them to be the same Y or N ) yesorno = string.upper(yesorno) if yesorno == 'Y': try: shutil.copy2(file_1, file_2) print copy has been done except: print it did not work The user prompt doesn't give the user much clue what he is agreeing to make the same but otherwise this seems OK. I assume you have write access to both files? #main file_name1 = open_files(test1.txt) file_name2 = open_files(test2.txt) file_name3 = open_files(test3.txt) the 3 variables above each contain not the *name* of the file but the contents. import shutil import string x = compare_files(file_name3, file_name1) x = cmp(f1,f2) would be just as useful. if x != 0: print test1 and test3 are different change_files(test1.txt, text3.txt, file_name1) The function above has 2 parameters but you are passing 3 arguments. I'd expect Python to complain at that? Also the first two are filenames but the last is the contents of file1 else: file_name1 = test2.txt but here you overwrite the contents with the filename print file_name1 and print text2.txt I'm not sure what thats trying to do? x = compare_files(file_name3, file_name2) again a simple cmp() would do the same job if x != 0: print test2 and test3 are different change_files(test2.txt, text3.txt) Since you didn't mention an error I assume your program always goes down this path? Despite the confusing names it looks like the program must be going down the second branch and trying to copy the two files. The only obvious thing I can think of is that the files do not have write permissions set appropriately? Not sure if that helps at all but the best I can do at 12:45am...:-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor