简体   繁体   English

os.system 在我的代码的前几行之前运行 Shell 命令

[英]os.system Runs Shell Command Before Previous Lines Of My Code

I have a code like this:我有这样的代码:

x = datetime.datetime.now()
with open ("lutfen_ol.log", 'a') as logluyoruz:
    logluyoruz.write("Islemler baslatiliyor.#####" + str(x) + "\n")
    os.system("stat log_deneme | grep Modify >> lutfen_ol.log")
    logluyoruz.write("\n")

I'm using this code for have a output like this:我正在使用此代码来获得这样的 output:

Islemler baslatiliyor.#####2021-02-06 20:23:13.995523
Modify: 2021-02-06 20:24:43.000959580 -0500

But it returns a result like this:但它返回如下结果:

Modify: 2021-02-06 20:23:08.056955577 -0500
Islemler baslatiliyor.#####2021-02-06 20:23:13.995523

How can I get a result like what I want?我怎样才能得到我想要的结果? os.system line is after writing "Islem baslatiliyor.#####" to my log file but it runs before this.. os.system 行是在将“Islem baslatiliyor.#####”写入我的日志文件之后,但它在此之前运行..

I tried to sleep my code before get stat of the "log_deneme" file's but it's not working.我试图在获取“log_deneme”文件的统计信息之前让我的代码休眠,但它不起作用。 Still running before..之前还在跑..

I did it guys..我做到了伙计们。。

with open ("lutfen_ol.log", 'a') as logluyoruz:
    logluyoruz.write("Islemler baslatiliyor.#####" + str(x) + "\n")
    logluyoruz.write("\n")

os.system("stat log_deneme | grep Modify >> lutfen_ol.log")

I tried this code and it worked.我尝试了这段代码,它奏效了。 I think the code in my question is not working like what I expected because it's in tab of with open .我认为我的问题中的代码没有像我预期的那样工作,因为它位于with open的选项卡中。

So it's answer of my question but not completely explaining source of the problem.所以这是我的问题的答案,但不能完全解释问题的根源。 It's only my guess.这只是我的猜测。

Thank you everyone.谢谢大家。

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

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