os.path.splitext() and case sensitivity

Discussion in 'Python' started by rbt, Dec 21, 2005.

  1. rbt

    rbt Guest

    Hi,

    Is there a way to make os.path.splitext() case agnostic?

    def remove_file_type(target_dir, file_type):
    for root, dirs, files in os.walk(target_dir):
    for f in files:
    if os.path.splitext(os.path.join(root, f))[1] in file_type:
    pass

    remove_file_type(sysroot, ['.tmp', '.TMP'])

    As you can see, the way I do it now, I place file extensions in a list.
    However, I'd like to able just to say '.tmp' and for that to work on any
    type of file that has tmp (no matter the case) in the extension.

    Many thanks!!!
    rbt, Dec 21, 2005
    #1
    1. Advertising

  2. rbt

    Juho Schultz Guest

    rbt wrote:
    > Hi,
    >
    > Is there a way to make os.path.splitext() case agnostic?
    >
    > def remove_file_type(target_dir, file_type):
    > for root, dirs, files in os.walk(target_dir):
    > for f in files:
    > if os.path.splitext(os.path.join(root, f))[1] in file_type:
    > pass
    >
    > remove_file_type(sysroot, ['.tmp', '.TMP'])
    >
    > As you can see, the way I do it now, I place file extensions in a list.
    > However, I'd like to able just to say '.tmp' and for that to work on any
    > type of file that has tmp (no matter the case) in the extension.
    >
    > Many thanks!!!



    One solution would be to convert the extensions to lowercase
    (or uppercase, if you prefer that)

    if fileExtension.lower() == ".tmp":
    Juho Schultz, Dec 21, 2005
    #2
    1. Advertising

  3. [rbt]
    > Is there a way to make os.path.splitext() case agnostic?
    >
    > def remove_file_type(target_dir, file_type):
    > for root, dirs, files in os.walk(target_dir):
    > for f in files:
    > if os.path.splitext(os.path.join(root, f))[1] in file_type:
    > pass
    >
    > remove_file_type(sysroot, ['.tmp', '.TMP'])


    def remove_file_type(target_dir, file_type):
    [...]
    if os.path.splitext(f)[1].lower() == file_type.lower():
    pass

    remove_file_type(sysroot, '.tmp')

    --
    Richie Hindle
    Richie Hindle, Dec 21, 2005
    #3
  4. rbt

    rbt Guest

    Richie Hindle wrote:
    > [rbt]
    >> Is there a way to make os.path.splitext() case agnostic?
    >>
    >> def remove_file_type(target_dir, file_type):
    >> for root, dirs, files in os.walk(target_dir):
    >> for f in files:
    >> if os.path.splitext(os.path.join(root, f))[1] in file_type:
    >> pass
    >>
    >> remove_file_type(sysroot, ['.tmp', '.TMP'])

    >
    > def remove_file_type(target_dir, file_type):
    > [...]
    > if os.path.splitext(f)[1].lower() == file_type.lower():
    > pass
    >
    > remove_file_type(sysroot, '.tmp')
    >


    Thanks guys!!!
    rbt, Dec 21, 2005
    #4
  5. rbt

    rbt Guest

    Juho Schultz wrote:
    > rbt wrote:
    >> Hi,
    >>
    >> Is there a way to make os.path.splitext() case agnostic?
    >>
    >> def remove_file_type(target_dir, file_type):
    >> for root, dirs, files in os.walk(target_dir):
    >> for f in files:
    >> if os.path.splitext(os.path.join(root, f))[1] in file_type:
    >> pass
    >>
    >> remove_file_type(sysroot, ['.tmp', '.TMP'])
    >>
    >> As you can see, the way I do it now, I place file extensions in a
    >> list. However, I'd like to able just to say '.tmp' and for that to
    >> work on any type of file that has tmp (no matter the case) in the
    >> extension.
    >>
    >> Many thanks!!!

    >
    >
    > One solution would be to convert the extensions to lowercase
    > (or uppercase, if you prefer that)
    >
    > if fileExtension.lower() == ".tmp":


    Many thanks... I did it this way as I sometimes delete files with
    different extensions:

    def remove_file_type(target_dir, file_type):
    for root, dirs, files in os.walk(target_dir):
    for f in files:
    if os.path.splitext(os.path.join(root, f))[1].lower() in
    file_type:


    remove_file_type(user_docs, ['.tmp', '.mp3'])
    rbt, Dec 21, 2005
    #5
    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. Harry Whitehouse
    Replies:
    0
    Views:
    290
    Harry Whitehouse
    Sep 15, 2004
  2. Replies:
    0
    Views:
    453
  3. Kev
    Replies:
    15
    Views:
    832
    Lunchbox G4
    Jan 3, 2005
  4. Roy Schestowitz

    Case Sensitivity in Linux Server

    Roy Schestowitz, Mar 23, 2005, in forum: HTML
    Replies:
    10
    Views:
    735
    Roy Schestowitz
    Mar 24, 2005
  5. asqui
    Replies:
    0
    Views:
    251
    asqui
    Aug 17, 2004
Loading...

Share This Page