0

I want read all the files in certain folder one by one. I performed this code

import os
Folder='/home/tanvir/'
FolderList = ['N10fine1Sh0.3S0.1/']
for Loop in FolderList:
    folderN = folder + Loop + str(0) + '.py'
    if os.path.exists(folderN):
        execfile(folderN)
    else:
        print('\nNo file 0.py in the folder precised, possible mistake on the folder name ! : {0}\n'.format(folderN))
        exit()

And I get this error.

python tanvirtest.py 
Traceback (most recent call last):
  File "tanvirtest.py", line 55, in <module>
    folderN = folder + Loop + str(0) + '.py'
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Ivan Kolesnikov
  • 1,787
  • 1
  • 29
  • 45
  • Possible duplicate of [Using os.walk() to recursively traverse directories in Python](http://stackoverflow.com/questions/16953842/using-os-walk-to-recursively-traverse-directories-in-python) – keepAlive Apr 03 '17 at 21:56
  • Or https://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory – alpoza Apr 03 '17 at 21:57

1 Answers1

0

Python is not recognizing one of the variables in the formula

folderN = folder + Loop + str(0) + '.py'

What is the variable folder? Is it supposed to be the capitalized variable Folder? Incorrect capitalization might be the culprit here.

Try changing Folder='/home/tanvir/' to folder='/home/tanvir/' and try again.

BenWurth
  • 790
  • 1
  • 7
  • 23
  • Hi it should be Folder in both. It works fine now. Offtopic: If I use folder in both cases it still gives that error. –  Apr 03 '17 at 22:05
  • @TanvirHossain Glad to hear that it's working! Could you please mark the answer as correct? That way it can help others with similar problems. – BenWurth Apr 04 '17 at 00:36