Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through files in a folder

Tags:

python

file

loops

I'm fairly new when it comes to programming, and have started out learning python.

What I want to do is to recolour sprites for a game, and I am given the original colours, followed by what they are to be turned into. Each sprite has between 20 and 60 angles, so looping through each one in the folder for each colour is probably the way to go for me. My code goes thusly;

import media import sys import os.path  original_colors = str(raw_input('Please enter the original RGB component, separated ONLY by a single space: ')) new_colors = str(raw_input('Please insert the new RGB component, separated ONLY by a single space: ')) original_list = original_colors.split(' ') new_list = new_colors.split(' ') folder = 'C:\Users\Spriting\blue' if original_colors == 'quit' or new_colors == 'quit':     sys.exit(0) else:     while 1:         for filename in os.listdir (folder):             for pix in filename:                 if (media.get_red(pix) == int(original_list[0])) and (media.get_green(pix) == int(original_list[1])) and \                    (media.get_blue(pix) == int(original_list[2])):                     media.set_red(pix, new_list[0])                     media.set_green(pix, new_list[1])                     media.set_blue(pix, new_list[2])                      media.save(pic) 

But I keep getting an error on the pathname, and on pix being a string value (They're all pictures)

Any help appreciated.

like image 428
Firas Avatar asked Nov 24 '09 19:11

Firas


People also ask

Can you loop through a file?

Because text files are sequences of lines of text, we can use the for loop to iterate through each line of the file. A line of a file is defined to be a sequence of characters up to and including a special character called the newline character.

Which loop can be used to iterate over files in a directory?

walk(), and glob module are the methods available to iterate over files. A directory is also known as a folder. It is a collection of files and subdirectories. The module os is useful to work with directories.

How do you loop through all the files in a directory in bash?

The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).


1 Answers

os.listdir() returns a list of file names. Thus, filename is a string. You need to open the file before iterating on it, I guess.

Also, be careful with backslashes in strings. They are mostly used for special escape sequences, so you need to escape them by doubling them. You could use the constant os.sep to be more portable, or even use os.path.join() :

folder = os.path.join('C:\\', 'Users', 'Sprinting', 'blue') 
like image 57
Wookai Avatar answered Oct 04 '22 16:10

Wookai