Friday 15 June 2012

osx - Python filename change -



osx - Python filename change -

i have number of videos in directory on mac have specific string in file name want remove, want maintain rest of file name is. i'm running python script terminal.

i have syntax doesn't seem work. practical utilize following? seems simple best way sort of thing why don't think works.

from os import rename, listdir text = "some text want remove file name" files = listdir("/users/admin/desktop/dir_of_videos/") x in files: if text in files: os.rename(files, files.replace(text, ""))

the problem incomplete paths when using listdir, basically, returns files in directory without prepending path directory

this should job:

import os in_dir = './test' remove = 'hello' paths = [os.path.join(in_dir,file) file in os.listdir(in_dir) if remove in file] file in paths: os.rename(file, file.replace(remove, ""))

python osx

No comments:

Post a Comment