Renaming of files in OS directory

Discussion in 'Python' started by blur959, Aug 8, 2010.

  1. blur959

    blur959 Guest

    Hi, all, I am writing a program that renames files inside OS
    directories the user provides. I am at the early stage of writing it
    and I encountered some problems.

    Below is my code. There is an error i received when i run this code.
    The error is, WindowsError: [Error 123] The filename, directory name,
    or volume label syntax is incorrect.

    Hope you guys could help.



    import os, glob

    def fileDirectory():
    #Asks the user for a file root directory
    fileroot = raw_input("Please input the file root directory \n\n")
    print fileroot

    #Returns a list with all the files inside the file root directory
    os.listdir(fileroot)

    fileDirectory()
     
    blur959, Aug 8, 2010
    #1
    1. Advertising

  2. blur959

    Chris Rebert Guest

    On Sun, Aug 8, 2010 at 1:02 AM, blur959 <> wrote:
    > Hi, all, I am writing a program that renames files inside OS
    > directories the user provides. I am at the early stage of writing it
    > and I encountered some problems.
    >
    > Below is my code. There is an error i received when i run this code.
    > The error is, WindowsError: [Error 123] The filename, directory name,
    > or volume label syntax is incorrect.


    Well, what directory did you input? Apparently it wasn't a valid or extant one.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Aug 8, 2010
    #2
    1. Advertising

  3. blur959

    blur959 Guest

    On Aug 8, 4:15 pm, Chris Rebert <> wrote:
    > On Sun, Aug 8, 2010 at 1:02 AM, blur959 <> wrote:
    > > Hi, all, I am writing a program that renames files inside OS
    > > directories the user provides. I am at the early stage of writing it
    > > and I encountered some problems.

    >
    > > Below is my code. There is an error i received when i run this code.
    > > The error is, WindowsError: [Error 123] The filename, directory name,
    > > or volume label syntax is incorrect.

    >
    > Well, what directory did you input? Apparently it wasn't a valid or extant one.
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com



    I input for e.g, "C:" it works, basically, if i input a hard code
    string inside os.listdir it works, but if i stored the string that the
    user keyed inside a variable and run os.listdir with the variable,
    there is that error. But inputing hard code string inside os.listdir
    isn't what I want when I am writing this program.
     
    blur959, Aug 8, 2010
    #3
  4. On 08/08/2010 10:35 AM, blur959 wrote:
    > On Aug 8, 4:15 pm, Chris Rebert <> wrote:
    >> On Sun, Aug 8, 2010 at 1:02 AM, blur959 <> wrote:
    >>> Hi, all, I am writing a program that renames files inside OS
    >>> directories the user provides. I am at the early stage of writing it
    >>> and I encountered some problems.

    >>
    >>> Below is my code. There is an error i received when i run this code.
    >>> The error is, WindowsError: [Error 123] The filename, directory name,
    >>> or volume label syntax is incorrect.

    >>
    >> Well, what directory did you input? Apparently it wasn't a valid or extant one.
    >>
    >> Cheers,
    >> Chris
    >> --http://blog.rebertia.com

    >
    >
    > I input for e.g, "C:" it works, basically, if i input a hard code
    > string inside os.listdir it works, but if i stored the string that the
    > user keyed inside a variable and run os.listdir with the variable,
    > there is that error. But inputing hard code string inside os.listdir
    > isn't what I want when I am writing this program.


    You didn't answert the question. What is the actual string you pass to
    os.listdir after you got it from the user? You could
    print repr(fileroot)
    to find out.

    My tentative guess is that maybe Windows doesn't like newlines in file
    names (I know UNIX allows them, but they're still usually a bad idea)
    and maybe you string ends with a newline.
     
    Thomas Jollans, Aug 8, 2010
    #4
  5. blur959

    blur959 Guest

    On Aug 8, 6:05 pm, Thomas Jollans <> wrote:
    > On 08/08/2010 10:35 AM, blur959 wrote:
    >
    >
    >
    > > On Aug 8, 4:15 pm, Chris Rebert <> wrote:
    > >> On Sun, Aug 8, 2010 at 1:02 AM, blur959 <> wrote:
    > >>> Hi, all, I am writing a program that renames files inside OS
    > >>> directories the user provides. I am at the early stage of writing it
    > >>> and I encountered some problems.

    >
    > >>> Below is my code. There is an error i received when i run this code.
    > >>> The error is, WindowsError: [Error 123] The filename, directory name,
    > >>> or volume label syntax is incorrect.

    >
    > >> Well, what directory did you input? Apparently it wasn't a valid or extant one.

    >
    > >> Cheers,
    > >> Chris
    > >> --http://blog.rebertia.com

    >
    > > I input for e.g, "C:" it works, basically, if i input a hard code
    > > string inside os.listdir it works, but if i stored the string that the
    > > user keyed inside a variable and run os.listdir with the variable,
    > > there is that error. But inputing hard code string inside os.listdir
    > > isn't what I want when I am writing this program.

    >
    > You didn't answert the question. What is the actual string you pass to
    > os.listdir after you got it from the user? You could
    >     print repr(fileroot)
    > to find out.
    >
    > My tentative guess is that maybe Windows doesn't like newlines in file
    > names (I know UNIX allows them, but they're still usually a bad idea)
    > and maybe you string ends with a newline.




    I do not get what you mean. The string i passed in is stored inside
    the variable fileroot. In the case I tested, i inputed the string "C:
    \" inside the raw_input and stored it inside fileroot, I tried
    printing repr(fileroot) and it gave me "C:\" as the result and when i
    tried running os.listdir(fileroot) i got the error. The string i
    passed to os.listdir is the string i keyed inside fileroot under the
    raw_input?
     
    blur959, Aug 8, 2010
    #5
  6. On 08/08/2010 12:23 PM, blur959 wrote:
    > On Aug 8, 6:05 pm, Thomas Jollans <> wrote:
    >> On 08/08/2010 10:35 AM, blur959 wrote:
    >>
    >>
    >>
    >>> On Aug 8, 4:15 pm, Chris Rebert <> wrote:
    >>>> On Sun, Aug 8, 2010 at 1:02 AM, blur959 <> wrote:
    >>>>> Hi, all, I am writing a program that renames files inside OS
    >>>>> directories the user provides. I am at the early stage of writing it
    >>>>> and I encountered some problems.

    >>
    >>>>> Below is my code. There is an error i received when i run this code.
    >>>>> The error is, WindowsError: [Error 123] The filename, directory name,
    >>>>> or volume label syntax is incorrect.

    >>
    >>>> Well, what directory did you input? Apparently it wasn't a valid or extant one.

    >>
    >>>> Cheers,
    >>>> Chris
    >>>> --http://blog.rebertia.com

    >>
    >>> I input for e.g, "C:" it works, basically, if i input a hard code
    >>> string inside os.listdir it works, but if i stored the string that the
    >>> user keyed inside a variable and run os.listdir with the variable,
    >>> there is that error. But inputing hard code string inside os.listdir
    >>> isn't what I want when I am writing this program.

    >>
    >> You didn't answert the question. What is the actual string you pass to
    >> os.listdir after you got it from the user? You could
    >> print repr(fileroot)
    >> to find out.
    >>
    >> My tentative guess is that maybe Windows doesn't like newlines in file
    >> names (I know UNIX allows them, but they're still usually a bad idea)
    >> and maybe you string ends with a newline.

    >
    >
    >
    > I do not get what you mean. The string i passed in is stored inside
    > the variable fileroot. In the case I tested, i inputed the string "C:
    > \" inside the raw_input and stored it inside fileroot, I tried
    > printing repr(fileroot) and it gave me "C:\" as the result and when i
    > tried running os.listdir(fileroot) i got the error. The string i
    > passed to os.listdir is the string i keyed inside fileroot under the
    > raw_input?


    You are passing a string to os.listdir. (you call that string fileroot).
    There is probably something wrong with that string. In principle, it
    doesn't matter where you got the string from - with raw_input() or by
    hard-coding the string.

    repr(fileroot) is almost certainly not "C:\" -- that is not a valid
    string literal.

    What did you enter exactly?

    -- Thomas
     
    Thomas Jollans, Aug 8, 2010
    #6
  7. blur959

    blur959 Guest

    On Aug 8, 7:45 pm, Thomas Jollans <> wrote:
    > On 08/08/2010 12:23 PM, blur959 wrote:
    >
    >
    >
    > > On Aug 8, 6:05 pm, Thomas Jollans <> wrote:
    > >> On 08/08/2010 10:35 AM, blur959 wrote:

    >
    > >>> On Aug 8, 4:15 pm, Chris Rebert <> wrote:
    > >>>> On Sun, Aug 8, 2010 at 1:02 AM, blur959 <> wrote:
    > >>>>> Hi, all, I am writing a program that renames files inside OS
    > >>>>> directories the user provides. I am at the early stage of writing it
    > >>>>> and I encountered some problems.

    >
    > >>>>> Below is my code. There is an error i received when i run this code..
    > >>>>> The error is, WindowsError: [Error 123] The filename, directory name,
    > >>>>> or volume label syntax is incorrect.

    >
    > >>>> Well, what directory did you input? Apparently it wasn't a valid or extant one.

    >
    > >>>> Cheers,
    > >>>> Chris
    > >>>> --http://blog.rebertia.com

    >
    > >>> I input for e.g, "C:" it works, basically, if i input a hard code
    > >>> string inside os.listdir it works, but if i stored the string that the
    > >>> user keyed inside a variable and run os.listdir with the variable,
    > >>> there is that error. But inputing hard code string inside os.listdir
    > >>> isn't what I want when I am writing this program.

    >
    > >> You didn't answert the question. What is the actual string you pass to
    > >> os.listdir after you got it from the user? You could
    > >>     print repr(fileroot)
    > >> to find out.

    >
    > >> My tentative guess is that maybe Windows doesn't like newlines in file
    > >> names (I know UNIX allows them, but they're still usually a bad idea)
    > >> and maybe you string ends with a newline.

    >
    > > I do not get what you mean. The string i passed in is stored inside
    > > the variable fileroot. In the case I tested, i inputed the string "C:
    > > \" inside the raw_input and stored it inside fileroot, I tried
    > > printing repr(fileroot) and it gave me "C:\" as the result and when i
    > > tried running os.listdir(fileroot) i got the error. The string i
    > > passed to os.listdir is the string i keyed inside fileroot under the
    > > raw_input?

    >
    > You are passing a string to os.listdir. (you call that string fileroot).
    > There is probably something wrong with that string. In principle, it
    > doesn't matter where you got the string from - with raw_input() or by
    > hard-coding the string.
    >
    > repr(fileroot) is almost certainly not "C:\" -- that is not a valid
    > string literal.
    >
    > What did you enter exactly?
    >
    > -- Thomas




    Sorry, This is my first time using the os commands in python, Ok,
    firstly, I entered "C:\" inside raw_input and stored it inside
    fileroot. When i print repr(fileroot), my result was '"C:\\"' . And
    when I run os.listdir with fileroot, I got that error. I typed my
    os.listdir code like this: os.listdir(fileroot)

    I attached my code for reference, thanks again!


    import os, glob

    def fileDirectory():
    # Ask user for file directory input
    fileroot = raw_input("Input")
    print repr(fileroot)


    #Returns a list with all the files inside the file root
    directory( The error occurs here )
    os.listdir(fileroot)


    fileDirectory()
     
    blur959, Aug 8, 2010
    #7
  8. On 08/08/2010 02:35 PM, blur959 wrote:
    > Sorry, This is my first time using the os commands in python, Ok,
    > firstly, I entered "C:\" inside raw_input and stored it inside
    > fileroot. When i print repr(fileroot), my result was '"C:\\"' . And
    > when I run os.listdir with fileroot, I got that error. I typed my
    > os.listdir code like this: os.listdir(fileroot)
    >
    > I attached my code for reference, thanks again!


    Okay, maybe you've already understood the problem now, but in case you
    haven't:
    '"C:\\"' is no valid file name. The quotes ("") are part of what you're
    passing to the OS here, which you don't want. Just enter the file name
    without quotes. (or, if you really want quoting for some reason, you
    could manually strip the quotes, or use the shlex module.

    >
    >
    > import os, glob
    >
    > def fileDirectory():
    > # Ask user for file directory input
    > fileroot = raw_input("Input")
    > print repr(fileroot)
    >
    >
    > #Returns a list with all the files inside the file root
    > directory( The error occurs here )
    > os.listdir(fileroot)
    >
    >
    > fileDirectory()
     
    Thomas Jollans, Aug 8, 2010
    #8
  9. blur959

    blur959 Guest

    On Aug 8, 9:13 pm, Thomas Jollans <> wrote:
    > On 08/08/2010 02:35 PM, blur959 wrote:
    >
    > > Sorry, This is my first time using the os commands in python, Ok,
    > > firstly, I entered "C:\" inside raw_input and stored it inside
    > > fileroot. When i print repr(fileroot), my result was '"C:\\"' . And
    > > when I run os.listdir with fileroot, I got that error. I typed my
    > > os.listdir code like this: os.listdir(fileroot)

    >
    > > I attached my code for reference, thanks again!

    >
    > Okay, maybe you've already understood the problem now, but in case you
    > haven't:
    > '"C:\\"' is no valid file name. The quotes ("") are part of what you're
    > passing to the OS here, which you don't want. Just enter the file name
    > without quotes. (or, if you really want quoting for some reason, you
    > could manually strip the quotes, or use the shlex module.
    >
    >
    >
    > > import os, glob

    >
    > > def fileDirectory():
    > >     # Ask user for file directory input
    > >     fileroot = raw_input("Input")
    > >     print repr(fileroot)

    >
    > >     #Returns a list with all the files inside the file root
    > > directory( The error occurs here )
    > >     os.listdir(fileroot)

    >
    > > fileDirectory()

    >
    >


    okay i got it already thanks alot man! Appreciate it!
     
    blur959, Aug 8, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Terrazas

    renaming files using asp

    Roy Terrazas, Jul 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    379
    Craig Deelsnyder
    Jul 30, 2003
  2. master007
    Replies:
    8
    Views:
    849
    Oliver Wong
    Mar 6, 2006
  3. Colin

    Renaming files in C

    Colin, Jul 4, 2003, in forum: C Programming
    Replies:
    3
    Views:
    6,182
    Peter Shaggy Haywood
    Jul 6, 2003
  4. Robizzle
    Replies:
    5
    Views:
    552
    Neil Cerutti
    Nov 21, 2005
  5. Marten Lehmann

    moving/renaming directory between NFS mounts

    Marten Lehmann, Nov 21, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    160
    Marten Lehmann
    Nov 21, 2008
Loading...

Share This Page