It would be helpfull to show where in the code the error messages are produced and describe the variable "it" and what "it" is...

I'm making a guess: when the filenames are lines of a text file, they include the end-of-line character and thus are incorrect.