![]() ![]() ![]() This is because the MD5 function needs to read the file as a sequence of bytes. The file is opened in rb mode, which means that you are going to read the file in binary mode. If re.match(r'.*?\.txt$', file) is not None:Ĭount = len( re.findall( self. with open ('myfile.jpg', 'rb') as afile: buf afile.read () hasher.update (buf) print (hasher.hexdigest ()) /python The code above calculates the MD5 digest of the file. (Как искать часть текста в файле и возвращять путь к файлу в котором это слово найдено) import osįor root, dirs, files in os.walk( self.path ): with open('testfile.txt', 'r') as infile: text infile.read() with open('outputtestfile.txt', 'w') as outfile: outfile.write(replaceall(text, spellingdict)) Edit: To make your code correctly handle words that contain other words (like 'entire' containing 'tire'), you probably need to abandon the simple str.replace approach in favor of regular expressions. How to search the text in the file and Returns an file path in which the word is found You could also use regular expressions on mmap e.g., case-insensitive search: if re.search(br'(?i)blabla', s): ![]() Mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s: With open('example.txt', 'rb', 0) as file, \ s.find(b'blabla'): #!/usr/bin/env python3 NOTE: in python 3, mmaps behave like bytearray objects rather than strings, so the subsequence you look for with find() has to be a bytes object rather than a string as well, eg. S = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line): with open('example.txt') as f:Īnother trick: you can alleviate the possible memory problems by using mmap.mmap() to create a "string-like" object that uses the underlying file (instead of reading the whole file in memory): import mmap ![]() The reason why you always got True has already been given, so I'll just offer another suggestion: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |