simple renaming files program

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

  1. blur959

    blur959 Guest

    Hi, all, I am working on a simple program that renames files based on
    the directory the user gives, the names the user searched and the
    names the user want to replace. However, I encounter some problems.
    When I try running the script, when it gets to the os.rename part,
    there will be an error. The error is :
    n = os.rename(file, file.replace(s, r))
    WindowsError: [Error 2] The system cannot find the file specified

    I attached my code below, hope you guys can help me, Thanks!

    import os
    directory = raw_input("input file directory")
    s = raw_input("search for name")
    r = raw_input("replace name")

    for file in os.listdir(directory):
    n = os.rename(file, file.replace(s, r))
    print n
     
    blur959, Aug 9, 2010
    #1
    1. Advertising

  2. blur959

    Chris Rebert Guest

    On Mon, Aug 9, 2010 at 2:44 AM, blur959 <> wrote:
    > Hi, all, I am working on a simple program that renames files based on
    > the directory the user gives, the names the user searched and the
    > names the user want to replace. However, I encounter some problems.
    > When I try running the script, when it gets to the os.rename part,
    > there will be an error. The error is :
    >  n = os.rename(file, file.replace(s, r))
    > WindowsError: [Error 2] The system cannot find the file specified
    >
    > I attached my code below, hope you guys can help me, Thanks!
    >
    > import os
    > directory = raw_input("input file directory")
    > s = raw_input("search for name")
    > r = raw_input("replace name")
    >
    > for file in os.listdir(directory):
    >    n = os.rename(file, file.replace(s, r))
    >    print n


    os.rename() takes paths that are absolute (or possibly relative to the
    cwd), not paths that are relative to some arbitrary directory (as
    returned by os.listdir()).
    Also, never name a variable "file"; it shadows the name of the built-in type.

    Hence (untested):
    from os import listdir, rename
    from os.path import isdir, join
    directory = raw_input("input file directory")
    s = raw_input("search for name")
    r = raw_input("replace name")

    for filename in listdir(directory):
    path = join(directory, filename) #paste the directory name on
    if isdir(path): continue #skip subdirectories (they're not files)
    newname = filename.replace(s, r)
    newpath = join(directory, newname)
    n = rename(path, newpath)
    print n

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

  3. blur959 wrote:
    > Hi, all, I am working on a simple program that renames files based on
    > the directory the user gives, the names the user searched and the
    > names the user want to replace. However, I encounter some problems.
    > When I try running the script, when it gets to the os.rename part,
    > there will be an error. The error is :
    > n = os.rename(file, file.replace(s, r))
    > WindowsError: [Error 2] The system cannot find the file specified


    I see that you are using os.listdir(), so the files should be present, but
    still, I would consider checking that when I encounter this error.

    > I attached my code below, hope you guys can help me, Thanks!
    >
    > import os
    > directory = raw_input("input file directory")
    > s = raw_input("search for name")
    > r = raw_input("replace name")
    >
    > for file in os.listdir(directory):
    > n = os.rename(file, file.replace(s, r))
    > print n


    Looks good so far, but what are the values in s, r, file and the result of
    file.replace(s, r) for the case that fails? Also, as a side note,
    help(os.rename) doesn't document any returnvalue to store in n, but that
    doesn't seem to be the problem.

    Uli


    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
     
    Ulrich Eckhardt, Aug 9, 2010
    #3
  4. blur959

    Peter Otten Guest

    Chris Rebert wrote:

    > Hence (untested):
    > from os import listdir, rename
    > from os.path import isdir, join
    > directory = raw_input("input file directory")
    > s = raw_input("search for name")
    > r = raw_input("replace name")
    >
    > for filename in listdir(directory):
    > path = join(directory, filename) #paste the directory name on
    > if isdir(path): continue #skip subdirectories (they're not files)
    > newname = filename.replace(s, r)
    > newpath = join(directory, newname)
    > n = rename(path, newpath)
    > print n


    Warning: I don't remember how Windows handles this, but unix will happily
    perform os.rename("alpha/alpha.txt", "beta/beta.txt") and overwrite
    beta/beta.txt with alpha/alpha.txt.

    I'd rather modify the filename before joining it with the directory.

    newname = filename.replace(s, r)
    if newname != filename:
    path = os.path.join(directory, filename)
    newpath = os.path.join(directory, newname)
    os.rename(path, newpath)

    If you don't you run the risk of operating in unexpected directories.

    Peter
     
    Peter Otten, Aug 9, 2010
    #4
  5. blur959

    blur959 Guest

    On Aug 9, 6:01 pm, Chris Rebert <> wrote:
    > On Mon, Aug 9, 2010 at 2:44 AM, blur959 <> wrote:
    > > Hi, all, I am working on a simple program that renames files based on
    > > the directory the user gives, the names the user searched and the
    > > names the user want to replace. However, I encounter some problems.
    > > When I try running the script, when it gets to the os.rename part,
    > > there will be an error. The error is :
    > >  n = os.rename(file, file.replace(s, r))
    > > WindowsError: [Error 2] The system cannot find the file specified

    >
    > > I attached my code below, hope you guys can help me, Thanks!

    >
    > > import os
    > > directory = raw_input("input file directory")
    > > s = raw_input("search for name")
    > > r = raw_input("replace name")

    >
    > > for file in os.listdir(directory):
    > >    n = os.rename(file, file.replace(s, r))
    > >    print n

    >
    > os.rename() takes paths that are absolute (or possibly relative to the
    > cwd), not paths that are relative to some arbitrary directory (as
    > returned by os.listdir()).
    > Also, never name a variable "file"; it shadows the name of the built-in type.
    >
    > Hence (untested):
    > from os import listdir, rename
    > from os.path import isdir, join
    > directory = raw_input("input file directory")
    > s = raw_input("search for name")
    > r = raw_input("replace name")
    >
    > for filename in listdir(directory):
    >     path = join(directory, filename) #paste the directory name on
    >     if isdir(path): continue #skip subdirectories (they're not files)
    >     newname = filename.replace(s, r)
    >     newpath = join(directory, newname)
    >     n = rename(path, newpath)
    >     print n
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com




    Thanks, they worked!
     
    blur959, Aug 9, 2010
    #5
  6. blur959

    Dave Angel Guest

    blur959 wrote:
    > On Aug 9, 6:01 pm, Chris Rebert <> wrote:
    >
    >> <snip>
    >> os.rename() takes paths that are absolute (or possibly relative to the
    >> cwd), not paths that are relative to some arbitrary directory (as
    >> returned by os.listdir()).
    >> Also, never name a variable "file"; it shadows the name of the built-in type.
    >>
    >> Hence (untested):
    >> from os import listdir, rename
    >> from os.path import isdir, join
    >> directory =aw_input("input file directory")
    >> s =aw_input("search for name")
    >> r =aw_input("replace name")
    >>
    >> for filename in listdir(directory):
    >> path = join(directory, filename) #paste the directory name on
    >> if isdir(path): continue #skip subdirectories (they're not files)
    >> newname = filename.replace(s, r)
    >> newpath = join(directory, newname)
    >> n = rename(path, newpath)
    >> print n
    >>
    >> Cheers,
    >> Chris
    >> --http://blog.rebertia.com
    >>

    >
    > Thanks, they worked!
    >
    >

    A refinement: use os.path.join(), rather than just join(). It's
    smarter about adding the right kind of slash between the nodes, if
    needed. Currently, if you leave off the trailing slash (from
    "directory"), you'll end up with the files being one level up, and the
    individual files having a string prepended.

    DaveA
     
    Dave Angel, Aug 9, 2010
    #6
  7. blur959

    MRAB Guest

    Dave Angel wrote:
    > blur959 wrote:
    >> On Aug 9, 6:01 pm, Chris Rebert <> wrote:
    >>
    >>> <snip>
    >>> os.rename() takes paths that are absolute (or possibly relative to the
    >>> cwd), not paths that are relative to some arbitrary directory (as
    >>> returned by os.listdir()).
    >>> Also, never name a variable "file"; it shadows the name of the
    >>> built-in type.
    >>>
    >>> Hence (untested):
    >>> from os import listdir, rename
    >>> from os.path import isdir, join
    >>> directory =aw_input("input file directory")
    >>> s =aw_input("search for name")
    >>> r =aw_input("replace name")
    >>>
    >>> for filename in listdir(directory):
    >>> path = join(directory, filename) #paste the directory name on
    >>> if isdir(path): continue #skip subdirectories (they're not files)
    >>> newname = filename.replace(s, r)
    >>> newpath = join(directory, newname)
    >>> n = rename(path, newpath)
    >>> print n
    >>>
    >>> Cheers,
    >>> Chris
    >>> --http://blog.rebertia.com
    >>>

    >>
    >> Thanks, they worked!
    >>
    >>

    > A refinement: use os.path.join(), rather than just join(). It's
    > smarter about adding the right kind of slash between the nodes, if
    > needed. Currently, if you leave off the trailing slash (from
    > "directory"), you'll end up with the files being one level up, and the
    > individual files having a string prepended.
    >

    Have a look at the imports, Dave. :)
     
    MRAB, Aug 9, 2010
    #7
  8. blur959

    Dave Angel Guest

    MRAB wrote:
    > <snip>
    >>>> from os.path import isdir, join
    >>>> <snip

    >>

    > Have a look at the imports, Dave. :)
    >

    Oops. I should have noticed that it was a function call, not a
    method. And there's no built-in called join(). I just usually avoid
    using this kind of alias, unless performance requires.

    thanks for keeping me honest.

    DaveA
     
    Dave Angel, Aug 9, 2010
    #8
  9. blur959

    Chris Rebert Guest

    On Mon, Aug 9, 2010 at 3:19 AM, Peter Otten <> wrote:
    > Chris Rebert wrote:
    >
    >> Hence (untested):
    >> from os import listdir, rename
    >> from os.path import isdir, join
    >> directory = raw_input("input file directory")
    >> s = raw_input("search for name")
    >> r = raw_input("replace name")
    >>
    >> for filename in listdir(directory):
    >>     path = join(directory, filename) #paste the directory name on
    >>     if isdir(path): continue #skip subdirectories (they're not files)
    >>     newname = filename.replace(s, r)
    >>     newpath = join(directory, newname)
    >>     n = rename(path, newpath)
    >>     print n

    >
    > Warning: I don't remember how Windows handles this, but unix will happily
    > perform os.rename("alpha/alpha.txt", "beta/beta.txt") and overwrite
    > beta/beta.txt with alpha/alpha.txt.
    >
    > I'd rather modify the filename before joining it with the directory.


    Er, unless I'm really missing something, that's what my code already
    does. Perhaps you misread it? The replace() clearly happens before the
    2nd join(). I took special care to account for and avoid the potential
    problem you're talking about.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Aug 9, 2010
    #9
  10. blur959

    Peter Otten Guest

    Chris Rebert wrote:

    > On Mon, Aug 9, 2010 at 3:19 AM, Peter Otten <> wrote:


    >> Warning: I don't remember how Windows handles this, but unix will happily
    >> perform os.rename("alpha/alpha.txt", "beta/beta.txt") and overwrite
    >> beta/beta.txt with alpha/alpha.txt.
    >>
    >> I'd rather modify the filename before joining it with the directory.

    >
    > Er, unless I'm really missing something, that's what my code already
    > does. Perhaps you misread it? The replace() clearly happens before the
    > 2nd join(). I took special care to account for and avoid the potential
    > problem you're talking about.


    You're right. Sorry for the confusion.

    Peter
     
    Peter Otten, Aug 9, 2010
    #10
    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:
    384
    Craig Deelsnyder
    Jul 30, 2003
  2. master007
    Replies:
    8
    Views:
    862
    Oliver Wong
    Mar 6, 2006
  3. Colin

    Renaming files in C

    Colin, Jul 4, 2003, in forum: C Programming
    Replies:
    3
    Views:
    6,208
    Peter Shaggy Haywood
    Jul 6, 2003
  4. Pegboy

    system(), renaming files and long filenames

    Pegboy, Nov 8, 2003, in forum: C Programming
    Replies:
    6
    Views:
    491
    Paul Emmons
    Dec 13, 2003
  5. David Lebel

    regular expressions and renaming files

    David Lebel, Feb 18, 2004, in forum: Python
    Replies:
    0
    Views:
    307
    David Lebel
    Feb 18, 2004
Loading...

Share This Page