anton.gridus...@gmail.com wrote: > Hello everyone! > > I need to find a file, that contains a string TeNum > > I try to > > import os > import sys > def find_value(fname): > value = 0 > with open(fname, encoding='cp866') as fn: > try: > for i in fn: > if 'TeNam' in i: > print(fname) > except IndexError: > pass
What's the purpose of that try...except? > return {fname} > def main(): > dirname = ('H:\\1\\3') > os.chdir(dirname) > res = {} For historical reasons {} is the literal for an empty dict; you probably want a set: res = set() > for i in os.listdir(dirname): > res.update(find_value(i)) > print('Filename is: ') > if __name__ == "__main__": > main() > > But there are mistakes like > C:\Users\Anton\AppData\Local\Programs\Python\Python36-32\python.exe > "C:/Users/Anton/PycharmProjects/Работа с файловой системой/Перебор файлов > из папки.py" Traceback (most recent call last): > File "C:/Users/Anton/PycharmProjects/Работа с файловой системой/Перебор > файлов из папки.py", line 21, in <module> > main() > File "C:/Users/Anton/PycharmProjects/Работа с файловой системой/Перебор > файлов из папки.py", line 18, in main > res.update(find_value(i)) > ValueError: dictionary update sequence element #0 has length 35; 2 is > required You get the error because the dict.update() methods expects pairs: >>> d = {} >>> d.update({(1, 2)}) >>> d {1: 2} A string of length 2 will be taken as a pair, >>> d.update({"xy"}) >>> d {1: 2, 'x': 'y'} but a string of any other length will not: >>> d.update({"xyz"}) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: dictionary update sequence element #0 has length 3; 2 is required > > Process finished with exit code 1 > > Could you help me to solve this thread? -- https://mail.python.org/mailman/listinfo/python-list