classification
Title: os.path.basename/split fails
Type: behavior Stage: resolved
Components: Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: kuiper, markon
Priority: normal Keywords:

Created on 2009-10-17 05:36 by kuiper, last changed 2009-10-17 14:21 by ezio.melotti. This issue is now closed.

Files
File name Uploaded Description Edit
newWF.py kuiper, 2009-10-17 05:36 python script in which error occurs
Messages (6)
msg94167 - (view) Author: Tom Kuiper (kuiper) Date: 2009-10-17 05:36
Normal behavior:
>>> from os import path
>>> filename 
= "/home/kuiper/Projects/microdischarges/Observing/2009-09-01/STATS_NP2000_VSR1A.1W1.09-244-193632"
>>> print filename
/home/kuiper/Projects/microdischarges/Observing/2009-09-01/STATS_NP2000_VSR1A.1W1.09-244-193632
>>> print path.basename(filename)
STATS_NP2000_VSR1A.1W1.09-244-193632

Abnormal behavior in class method:
from os import path
...
  def openDataFile(self):
    filename = QFileDialog.getOpenFileName(self, 'Open file','.')
    self.datafile.setTextpath.basename(filename))
...
Traceback (most recent call last):
  File "newWF.py", line 277, in openDataFile
    self.datafile.setText(os.path.basename(filename))
  File "/usr/lib/python2.5/posixpath.py", line 112, in basename
    return split(p)[1]
  File "/usr/lib/python2.5/posixpath.py", line 77, in split
    i = p.rfind('/') + 1
AttributeError: rfind
msg94173 - (view) Author: Marco Buccini (markon) Date: 2009-10-17 11:14
I think this is not a Python bug, since it concerns PyQt. 

You're passing a QString object to os.path.split(), while the official
documentation wants you pass a Python string.

However, when I tried to run your example, newWF.py, and tried to open a
file from the menu, I got this:

controls.nch: 1024                                                    
Controls Window data shape: (511, 1024)
Data file: None
/home/marco/Programmi/Python-2.6.3/pyconfig.h
Traceback (most recent call last):
  File "issue7158.py", line 277, in openDataFile
    head,tail = os.path.split(filename)
  File "/usr/lib/python2.5/posixpath.py", line 77, in split
    i = p.rfind('/') + 1
AttributeError: 'QString' object has no attribute 'rfind'

Why does this happen? 
`filename` is a QString object. (you can see why here:
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qfiledialog.html#getOpenFileName
)

So how do you split your filename? I don't know.

I should close this bug.
msg94174 - (view) Author: Marco Buccini (markon) Date: 2009-10-17 11:20
> I should close this bug.

I *would* close this bug. Sorry :)
msg94176 - (view) Author: Tom Kuiper (kuiper) Date: 2009-10-17 14:09
That's a good point, Marco.  I'd forgotten about the distinction.  
Converting 'filename' to a Python string with 'str()' fixed the problem.
> However, when I tried to run your example, newWF.py, and tried to open a
> file from the menu, I got this:
>
> controls.nch: 1024                                                    
> Controls Window data shape: (511, 1024)
> Data file: None
> /home/marco/Programmi/Python-2.6.3/pyconfig.h
> Traceback (most recent call last):
>   File "issue7158.py", line 277, in openDataFile
>     head,tail = os.path.split(filename)
>   File "/usr/lib/python2.5/posixpath.py", line 77, in split
>     i = p.rfind('/') + 1
> AttributeError: 'QString' object has no attribute 'rfind'
>   
I'm sorry about the confusion.  In the file I attached I had tried using 
'os.path.split()', with no luck.
> Why does this happen? 
> `filename` is a QString object. (you can see why here:
> http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qfiledialog.html#getOpenFileName
> )
>
> So how do you split your filename? I don't know.
>   
Python's 'split()' works, like this:
    basename = filename.split('/')[-1]
    self.datafile.setText(basename)
Also, with less code,
    self.datafile.setText(os.path.basename(str(filename)))
> I should close this bug.
>   
Yes.  Please do.  I guess it's useful though to have our e-mail exchange 
on record for anyone else who stumbles over the Python string vs QString 
distinction.

Thanks and regards

Tom
> ----------
> nosy: +markon
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue7158>
> _______________________________________
>
msg94177 - (view) Author: Marco Buccini (markon) Date: 2009-10-17 14:18
I cannot close this bug, ahah :)

BTW: I'm happy you solved this bug.
Bye.
msg94178 - (view) Author: Tom Kuiper (kuiper) Date: 2009-10-17 14:21
Do I have to do that?  I've never used the Python bug tracking system 
before.
> BTW: I'm happy you solved this bug.
>   
I guess we could consume a few beers over whether it's a bug or not.

Cheers

Tom
History
Date User Action Args
2009-10-17 14:21:51ezio.melottisetstatus: open -> closed
resolution: not a bug
stage: resolved
2009-10-17 14:21:19kuipersetmessages: + msg94178
2009-10-17 14:18:47markonsetmessages: + msg94177
2009-10-17 14:09:19kuipersetmessages: + msg94176
2009-10-17 11:20:20markonsetmessages: + msg94174
2009-10-17 11:14:47markonsetnosy: + markon
messages: + msg94173
2009-10-17 05:36:56kuipercreate