特定のディレクトリ (C:\files\) 内のすべての .txt ファイルをループしたいスレッドがあります。必要なのは、.txt ファイルであるそのディレクトリから何かを読み取ることだけです。私はそれを理解できないようです..特定のファイルを探す現在のコードは次のとおりです。
def file_Read(self):
if self.is_connected:
threading.Timer(5, self.file_Read).start();
print '~~~~~~~~~~~~Thread test~~~~~~~~~~~~~~~'
try:
with open('C:\\files\\test.txt', 'r') as content_file:
content = content_file.read()
Num,Message = content.strip().split(';')
print Num
print Message
print Num
self.send_message(Num + , Message)
content_file.close()
os.remove("test.txt")
#except
except Exception as e:
print 'no file ', e
time.sleep(10)
誰でもこれを簡単に修正できますか?次のような方法を使用して多くのスレッドを見つけました。
directory = os.path.join("c:\\files\\","path")
threading.Timer(5, self.file_Read).start();
print '~~~~~~~~~~~~Thread test~~~~~~~~~~~~~~~'
try:
for root,dirs,files in os.walk(directory):
for file in files:
if file.endswith(".txt"):
content_file = open(file, 'r')
しかし、これは機能していないようです。
どんな助けでも大歓迎です。前もって感謝します...