Temat:, Re: IOError: [Errno 22] invalid mode ('wb') or filename: in windows xp while making tarfile

Dave Angel davea at ieee.org
Tue Aug 25 02:09:10 CEST 2009


You shouldn't want either a colon or a pipe symbol in the filename 
string (not counting the drive letter prefix).
A single colon gives you surprising behavior, and two will give you 
Invalid Argument.   See inline responses.

ryniek wrote:
> On 24 Sie, 22:34, ryniek <rynie... at gmail.com> wrote:
>   
>>> <snip>
>>>       
>> Ok, but how to get rid of those colons?
>> Is it necessary?
>>     
>
> I dealt with it. Had to change 'w:bz2' into 'w|bz2'.
>
>   
Looks to me like you're putting a pipe in again (unless that's just an 
artifact of the mail system)
> But now have another problem:
> "
> C:\Users\Ryniek's WinSe7en\Documents\My Dropbox\Aplikacje
> \Moje_aplikacje\Pythonowe_aplikacje\Skrypty>python ba
> ckuper.py -f E:\APLIKACJE\nowegg.exe E:\ MyGG
> Checking permissions for reading and writing...
> Traceback (most recent call last):
>   File "backuper.py", line 194, in <module>
>     main_meth()
>   File "backuper.py", line 186, in main_meth
>     paq.backup_file(pars.options.filename[0], pars.options.filename
> [1], pars.options.filename[2])
>   File "backuper.py", line 114, in backup_file
>     backup_obj =arfile.open(dest, 'w|bz2')
>   File "E:\WinSe7en Apps\APLIKACJE\ActiveState Python 2.6\lib
> \tarfile.py", line 1675, in open
>     _Stream(name, filemode, comptype, fileobj, bufsize),
>   File "E:\WinSe7en Apps\APLIKACJE\ActiveState Python 2.6\lib
> \tarfile.py", line 400, in __init__
>     fileobj =LowLevelFile(name, mode)
>   File "E:\WinSe7en Apps\APLIKACJE\ActiveState Python 2.6\lib
> \tarfile.py", line 373, in __init__
>     self.fd =s.open(name, mode)
> OSError: [Errno 22] Invalid argument: 'E:\\MyGG(2009-08-24
> 23:18:25).tar.bz2'
> "
>
>   
Still have two erroneous colons in that string.  The one after the E is 
okay.
> When i type path with foreward slashes, Python converts them to double
> backslashes  :P
> "
> C:\Users\Ryniek's WinSe7en\Documents\My Dropbox\Aplikacje
> \Moje_aplikacje\Pythonowe_aplikacje\Skrypty>python backuper.py -f E:/
> APLIKACJE/nowegg.exe E:/ MyGG
> Checking permissions for reading and writing...
> Traceback (most recent call last):
>   File "backuper.py", line 194, in <module>
>     main_meth()
>   File "backuper.py", line 186, in main_meth
>     paq.backup_file(pars.options.filename[0], pars.options.filename
> [1], pars.options.filename[2])
>   File "backuper.py", line 114, in backup_file
>     backup_obj =arfile.open(dest, 'w|bz2')
>   File "E:\WinSe7en Apps\APLIKACJE\ActiveState Python 2.6\lib
> \tarfile.py", line 1675, in open
>     _Stream(name, filemode, comptype, fileobj, bufsize),
>   File "E:\WinSe7en Apps\APLIKACJE\ActiveState Python 2.6\lib
> \tarfile.py", line 400, in __init__
>     fileobj =LowLevelFile(name, mode)
>   File "E:\WinSe7en Apps\APLIKACJE\ActiveState Python 2.6\lib
> \tarfile.py", line 373, in __init__
>     self.fd =s.open(name, mode)
> OSError: [Errno 22] Invalid argument: 'E:\\MyGG(2009-08-24
> 23:20:19).tar.bz2'
> "
>
> I tried raw string also, but nothing helps.   :-/
>
>   
If a string has a backslash in it, and it is printed, you see a single 
backslash.  But if it is output with repr(), then you see it doubled, 
just as you would do if you were typing it in as a literal.  That does 
NOT mean that it has a double backslash, it just means that particular 
display formatting shows you that.  Try the following to see what I mean:

 >>> st = r"e:\testing\again.txt"
 >>> st
'e:\\testing\\again.txt'
 >>> print st
e:\testing\again.txt
 >>> print [st, st]
['e:\\testing\\again.txt', 'e:\\testing\\again.txt']
 >>>

Since you don't know how a particular error message was constructed, you 
don't know for sure which way to interpret the results.  That comes with 
practice.  In the meantime, if you print a simple string with simple 
print, you should be able to see it fine.

Incidentally, in nearly every case, Windows ignores extra backslashes.  
So that's not likely to be your problem.  Get rid of those colons, 
without replacing them with pipes.

DaveA



More information about the Python-list mailing list