简体   繁体   English

Python 移动写完的日志文件

[英]Python moving log files that have finished writing

My job log file modified times are set as a job starts.我的作业日志文件修改时间设置为作业开始。 So if the job takes 20 minutes, the log file will be writing for those 20 minutes.因此,如果作业需要 20 分钟,则日志文件将在这 20 分钟内写入。

I need a script that says 'once the file is closed for writing (ie no more changes will be made), move it to another directory.我需要一个脚本,上面写着'一旦关闭文件以进行写入(即不再进行更改),将其移动到另一个目录。

Is there a way to do this in Python?有没有办法在 Python 中做到这一点? I can move files easy enough but how do I know the file is closed for writing?我可以很容易地移动文件,但我怎么知道文件已关闭以供写入?

Thanks a lot!非常感谢!

Something like this?像这样的东西?

The with statement will automatically close the file before moving on. with 语句将在继续之前自动关闭文件。

from shutil import move

with open('log.json') as myFile:
    .. do things

move('log.json', another_location)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM