Ben Keshet wrote:
it didn't help. it reads the pathway "as is" (see errors for both tries). It looks like it had the write pathway the first time, but could not find it because it searched in the path/way instead of in the path\way. thanks for trying.

The form of slash ('\' vs '/') is irrelevant to Python. At least on Windows.

folders= ['1','2','3']
for x in folders:
    print x     # print the current folder
    filename='Folder/%s/myfile.txt' %[x]
                                       ^- brackets not needed
    f=open(filename,'r')

gives: IOError: [Errno 2] No such file or directory: "Folder/['1']/myfile.txt"


Pay attention to the error message. Do you actually have the file "Folder\['1']\myfile.txt" on your machine? And did you really want brackets and quotes? Is the path located relative to whereever your python script is running from? If it's an absolute path, precede it with a leading slash.

As far as the Python question of string substitution, "%s" % var is an appropriate way.

Your above code should read:
folders = ['1', '2', '3']
for x in folders:
   print x
   filename = 'Folder/%s/myfile.txt' % x
   f = open(filename, 'r')

Again, in order for that to work, you *must* have a path/file of 'Folder\1\myfile.txt' existing from the same folder that this code is running from. This is O/S related, not Python related.
--
Ethan
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to