Python: rename files based on a Python list -


i have directory contains thousand or txt files. i've read , explored these files glob (searching specific strings within file) , i've appended filenames of files of interest list in python, so:

list_of_chosen_files = ['file2.txt', 'file10.txt', 'file17.txt', ...]

i'll using list other things well, i'm trying figure out how use os module cross-reference filenames in directory against list above and, if filename in list, add "1-" beginning of filename. i've saved "1-" in variable reuse well. here's have far: -

var = "1-"  import os filename in os.listdir("."):     if filename == list_of_chosen_files[:]:         os.rename(filename, var+filename)         print filename 

it's running without errors in anaconda, nothing's printing , none of files getting renamed. feel should such easy fix, i'm concerned poking around directories os module if don't know i'm doing yet.

any appreciated! thanks!

your issue line:

if filename == list_of_chosen_files[:]: 

you're comparing single string (filename) entire list (list_of_chosen_files[:] gives whole list). if want check if filename in list, use this:

if filename in list_of_chosen_files: 

this check see if list_of_chosen_files contains filename.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -