0

I have a directory full of video files that have been renamed to something like:

video.3gp~1209384

I need to remove all chars from the filename including the ~. Any help would be awesome!

user2836448
  • 1
  • 1
  • 1

2 Answers2

0

I would personally just use IPython for that; it has integration with bash/shell. Just prefix shell commands with ! and interpolate Python variables in shell commands using $.

>>> files = !ls *.3gp~*
>>> for f in files:
        newname = f.split('~')[0]
        mv $f $nename

Or, if that's not an option (e.g. you can't install IPython or must use pure Python):

import os

files = os.listdir(DIRNAME)
for f in files:
    if '.3gp~' in f:
        newname = f.split('~')[0]
        os.rename(f, newname)

Both of these assume your files don't have ~ in the part of the name you'd like to keep; it's very unlikely; but if they do, it'd be a really simple adaption in the code.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
0

Try this:

import os
for filename in os.listdir("."):    
    os.rename(filename, filename[:filename.find("~")])
Vivek
  • 910
  • 2
  • 9
  • 26