ディレクトリ内に「.tmp」という拡張子を持つフォルダを見つけようとしています。サブディレクトリ)。基本的には、特定のパスのどこにでも拡張子が「.tmp」のフォルダです。特定のディレクトリとそのサブディレクトリで、拡張子.tmpで終わるすべてのフォルダを見つけよう
現在のところ、特定のディレクトリでは拡張子.tmpのフォルダしか見つけられませんが、後続のディレクトリでは見つかりません。親切に助けてください。
コード:
def main():
"""The first function to be executed.
Changes the directory. In a particular directory and subdirectories, find
all the folders ending with .tmp extension and notify the user that it is
existing from a particular date.
"""
body = "Email body"
subject = "Subject for the email"
to_email = "[email protected]"
# Change the directory
os.chdir('/remote/us01home53/subburat/cn-alert/')
# print [name for name in os.listdir(".") if os.path.isdir(name)]
for name in os.listdir("."):
if os.path.isdir(name):
now = time.time()
if name.endswith('.tmp'):
if (now - os.path.getmtime(name)) > (1*60*60):
print('%s folder is older. Created at %s' %
(name, os.path.getmtime(name)))
print('Sending email...')
send_email(body, subject, to_email)
print('Email sent.')
if __name__ == '__main__':
main()
オペレーティングシステム:Linux;あなたがPythonの3.xを使用しているので プログラミング言語Pythonは
あなたは.tmpファイルのディレクトリ内の.tmpファイルを持つすべてのサブディレクトリのみのものを見つけたいですか?例えば[./test1.tmp、./test2/test3.tmp]対[./test1.tmp] – Robb
'os.walk'をチェックしてください。 –
@Robb '.tmp' extension.e.gを持つすべてのサブディレクトリを探したい。 [1.tmp、test/2.tmp、test1/test2/3.tmp]。 –