a simple 'for' question

B

Ben Keshet

Hi fans,

I want to use a 'for' iteration to manipulate files in a set of folders,
something like:

folders= ['1A28','1A6W','56Y7']
for x in folders:
print x # print the current folder
f = open('my/path/way/x/my_file.txt', 'r')
...

where 'x' in the pathway should iterate over '1A28','1A6W','56Y7'. How
should I identify 'x' in the pathway line as the same x that is
iterating over 'folders'?

I am getting the following error:

Traceback (most recent call last):
File
"C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Linux\Dock_method_validation\myscripts\test_for.py", line 5,
in <module>
f = open('c:/Linux/Dock_method_validation/x/receptor.mol2', 'r')
IOError: [Errno 2] No such file or directory:
'c:/Linux/Dock_method_validation/x/receptor.mol2'

I tired several variations: %x, 'x', "x", etc. all gave me similar errors.

Thanks for your help,
BK
 
C

cokofreedom

Hi fans,

I want to use a 'for' iteration to manipulate files in a set of folders,
something like:

folders= ['1A28','1A6W','56Y7']
for x in folders:
print x # print the current folder
f = open('my/path/way/x/my_file.txt', 'r')
...

where 'x' in the pathway should iterate over '1A28','1A6W','56Y7'. How
should I identify 'x' in the pathway line as the same x that is
iterating over 'folders'?

I am getting the following error:

Traceback (most recent call last):
File
"C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Linux\Dock_method_validation\myscripts\test_for.py", line 5,
in <module>
f = open('c:/Linux/Dock_method_validation/x/receptor.mol2', 'r')
IOError: [Errno 2] No such file or directory:
'c:/Linux/Dock_method_validation/x/receptor.mol2'

I tired several variations: %x, 'x', "x", etc. all gave me similar errors.

Thanks for your help,
BK
folders = ["a", "b", "c"]
for item in folders:
print item
file = open("my/path/way/" + item + "/my_file.txt", "r")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top