file/folder naming

E

ecu_jon

im trying to use wxpython to get a source file/files, then a
destination folder to write them to. the file and folder picker works.
the problem is, actually copying (actually shutil.copy2() ). i get an
except error, as if the destination file name is not correct. looking
at the print lines the destination looks right. i used os.path.join to
join the folder path and the filebasename. i can hear it now, that
this belongs on the wxpython page, and i will be posting there as
well. but, im fairly certain its something in this "for files in
sourcepath:"

import wx,os,string,shutil,getpass
from datetime import *

def backupfiles():
dialog = wx.FileDialog(None, "Choose a source file :", style=1 |
wx.MULTIPLE )
if dialog.ShowModal() == wx.ID_OK:
sourcepath = dialog.GetPaths()
else:
dialog.Destroy()
dialog = wx.DirDialog(None, "Choose a destination directory :",
style=1 )
if dialog.ShowModal() == wx.ID_OK:
destpath = dialog.GetPath()
else:
dialog.Destroy()
print "sourcepath is :",sourcepath
print "destpath is :",destpath
for files in sourcepath:
print "destpath
is :",os.path.join(destpath,os.path.basename(files))
try:
shutil.copy2(sourcepath,
os.path.join(destpath,os.path.basename(files)))
except:
print "error file"


class MyForm(wx.Frame):
"""make a frame, inherits wx.Frame"""
def __init__(self):
# create a frame, no parent, default to wxID_ANY
wx.Frame.__init__(self, None, wx.ID_ANY, title="FacBac",
pos=(300, 150), size=(500, 200))

self.SetBackgroundColour("purple")
panel = wx.Panel(self, wx.ID_ANY)
self.title1 = wx.StaticText(panel, wx.ID_ANY, "Backup")
self.title2 = wx.StaticText(panel, wx.ID_ANY, "Restore")

self.button1 = wx.Button(panel, id=-1, label='Backup ALL')
self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
self.button1.SetToolTip(wx.ToolTip("this will backup your
whole folder"))

gridSizer = wx.GridSizer(rows=4, cols=2, hgap=5, vgap=5)
gridSizer.Add(self.title1, 0, wx.ALL|wx.ALIGN_CENTER, 5)
gridSizer.Add(self.title2, 0, wx.ALL|wx.ALIGN_CENTER, 5)
gridSizer.Add(self.button1, 0, wx.ALL|wx.EXPAND, 5)

topSizer = wx.BoxSizer(wx.VERTICAL)
topSizer.Add(gridSizer, 0, wx.ALL|wx.EXPAND, 5)

self.SetSizeHints(500,200,500,200)

panel.SetSizer(topSizer)
topSizer.Fit(self)

def button1Click(self, event):
self.button1.SetLabel("Doing Backup")
backupfiles()
self.button1.SetLabel("Backup All")

if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
#import wx.lib.inspection
#wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
 
C

Chris Rebert

im trying to use wxpython to get a source file/files, then a
destination folder to write them to. the file and folder picker works.
the problem is, actually copying (actually shutil.copy2() ). i get an
except error

Please include the exact text of the error message and accompanying
Traceback. And always do so in the future.

Cheers,
Chris
 
S

Steven D'Aprano

im trying to use wxpython to get a source file/files, then a destination
folder to write them to. the file and folder picker works. the problem
is, actually copying (actually shutil.copy2() ). i get an except error,
as if the destination file name is not correct. looking at the print
lines the destination looks right. i used os.path.join to join the
folder path and the filebasename. i can hear it now, that this belongs
on the wxpython page, and i will be posting there as well. but, im
fairly certain its something in this "for files in sourcepath:"
try:
shutil.copy2(sourcepath,
os.path.join(destpath,os.path.basename(files)))
except:
print "error file"

There's your problem there: by hiding the *actual* error message Python
provides, and substituting a rubbish, useless, generic "error file"
message, you make it impossible to see what causes the error.

Don't do that. Until you fix that, you can't diagnose errors, because the
information needed to diagnose them is tossed away.
 
E

ecu_jon

Please include the exact text of the error message and accompanying
Traceback. And always do so in the future.

Cheers,
Chris

as i was using tr/except, there is no traceback, or i would have done
so.
the except error comes back and says it cant copy to the folder.
 
E

ecu_jon

ok changed the try/except to just a file copy and got this:
sourcepath is : [u'I:\\college\\spring11\\capstone-project\
\facbac-009.py']
destpath is : V:\week3
destpath is : V:\week3\facbac-009.py
Traceback (most recent call last):
File "I:\college\spring11\capstone-project\folder.py", line 53, in
button1Click
backupfiles()
File "I:\college\spring11\capstone-project\folder.py", line 19, in
backupfiles
shutil.copy2(sourcepath,
os.path.join(destpath,os.path.basename(files)))
File "C:\Python27\lib\shutil.py", line 127, in copy2
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 67, in copyfile
if _samefile(src, dst):
File "C:\Python27\lib\shutil.py", line 62, in _samefile
return (os.path.normcase(os.path.abspath(src)) ==
File "C:\Python27\lib\ntpath.py", line 471, in abspath
path = _getfullpathname(path)
TypeError: coercing to Unicode: need string or buffer, list found

yes i see the list.
but shouldnt this "for files in sourcepath:" parse thru the list ... ?
 
C

Chris Rebert

ok changed the try/except to just a file copy and got this:
sourcepath is : [u'I:\\college\\spring11\\capstone-project\
\facbac-009.py']
destpath is : V:\week3
destpath is : V:\week3\facbac-009.py
Traceback (most recent call last):
 File "I:\college\spring11\capstone-project\folder.py", line 53, in
button1Click
   backupfiles()
 File "I:\college\spring11\capstone-project\folder.py", line 19, in
backupfiles
   shutil.copy2(sourcepath,
os.path.join(destpath,os.path.basename(files)))
 File "C:\Python27\lib\shutil.py", line 127, in copy2
   copyfile(src, dst)
 File "C:\Python27\lib\shutil.py", line 67, in copyfile
   if _samefile(src, dst):
 File "C:\Python27\lib\shutil.py", line 62, in _samefile
   return (os.path.normcase(os.path.abspath(src)) ==
 File "C:\Python27\lib\ntpath.py", line 471, in abspath
   path = _getfullpathname(path)
TypeError: coercing to Unicode: need string or buffer, list found

yes i see the list.
but shouldnt this "for files in sourcepath:" parse thru the list ... ?

That doesn't matter when your actual copy2() call still refers to
`sourcepath` (the list):
shutil.copy2(sourcepath, os.path.join(destpath,os.path.basename(files)))
^^^^^^^^^^
I presume you want `files` here rather than `sourcepath`.

Incidentally, I think `sourcepath` is poorly named given that it's a
list of paths, and `files` is also poorly named given that it's a
single filepath.

Cheers,
Chris
 
S

Steven D'Aprano

ok changed the try/except to just a file copy and got this:
sourcepath is :
[u'I:\\college\\spring11\\capstone-project\\facbac-009.py']

Note that it's a list.
shutil.copy2(sourcepath,
os.path.join(destpath,os.path.basename(files)))

Note that you are calling copy2 with the first argument as a list.

TypeError: coercing to Unicode: need string or buffer, list found

Note that the error tells you exactly what is wrong.

yes i see the list.
but shouldnt this "for files in sourcepath:" parse thru the list ... ?

Yes it does. So what? You don't use files, you use sourcepath each time.

source = ['a', 'bb', 'ccc']
for x in source:
.... print(len(source))
....
3
3
3
 
E

ecu_jon

ok changed the try/except to just a file copy and got this:
sourcepath is :
[u'I:\\college\\spring11\\capstone-project\\facbac-009.py']

Note that it's a list.
    shutil.copy2(sourcepath,
                 os.path.join(destpath,os.path.basename(files)))

Note that you are calling copy2 with the first argument as a list.
TypeError: coercing to Unicode: need string or buffer, list found

Note that the error tells you exactly what is wrong.
yes i see the list.
but shouldnt this "for files in sourcepath:" parse thru the list ... ?

Yes it does. So what? You don't use files, you use sourcepath each time.
source = ['a', 'bb', 'ccc']
for x in source:

...     print(len(source))
...
3
3
3
so i changed it to
shutil.copy2(files, os.path.join(destpath,os.path.basename(files)))
seems to be working.
in the mornig when im not bleary eyed. ill check again but this look
like the fix. thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top