Why does this (very simple piece of) code does not work?

J

jblazi

import os

L = os.listdir('.')

for x in L:
print '!'+x+'!'+x[0]
if x != 'rename.py':
y = 'Kapustin (Hamelin): '+x
os.rename(x,y)

I get

Traceback (most recent call last):
File "<stdin>", line 9, in ?
OSError: [Errno 22] Invalid argument


TIA,

jb
 
M

Matteo Dell'Amico

jblazi said:
os.rename(x,y)
Traceback (most recent call last):
File "<stdin>", line 9, in ?
OSError: [Errno 22] Invalid argument

Works fine here (Linux). Maybe you're using a platform where you use
invalid characters for the filename?
 
R

Richard Brodie

jblazi said:
y = 'Kapustin (Hamelin): '+x
os.rename(x,y)
OSError: [Errno 22] Invalid argument

You didn't say what platform you are on but if it's Windows, then
colons aren't allowed in filenames.
 
P

Peter Hansen

jblazi said:
import os

L = os.listdir('.')

for x in L:
print '!'+x+'!'+x[0]
if x != 'rename.py':
y = 'Kapustin (Hamelin): '+x
os.rename(x,y)

I get

Traceback (most recent call last):
File "<stdin>", line 9, in ?
OSError: [Errno 22] Invalid argument

You didn't specify your OS, version of Python, etc,
but perhaps we can guess why it doesn't work anyway.

Have you tried executing the equivalent command manually,
either from the command line or from, say, Explorer (assuming
you're on Windows)? Can you rename any file to contain
parentheses and colons on your OS? If not, there's
your answer.

-Peter
 
C

Chris Cioffi

Just a guess here, but is ':' a valid filename character on your OS?
Most OSes seem to have it reserved for system use. Try using a
hyphen.

Chris

import os

L = os.listdir('.')

for x in L:
print '!'+x+'!'+x[0]
if x != 'rename.py':
y = 'Kapustin (Hamelin): '+x
os.rename(x,y)

I get

Traceback (most recent call last):
File "<stdin>", line 9, in ?
OSError: [Errno 22] Invalid argument

TIA,

jb
 
J

jblazi

You didn't say what platform you are on but if it's Windows, then
colons aren't allowed in filenames.

Yes. Now it seems, my age begins to tell, I did not used to be so dumb...
Thx.

jb
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top