【问题标题】:os.system Runs Shell Command Before Previous Lines Of My Codeos.system 在我的代码的前几行之前运行 Shell 命令
【发布时间】:2021-02-07 01:40:39
【问题描述】:

我有这样的代码:

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")

我使用这段代码来得到这样的输出:

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

但它返回的结果是这样的:

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

我怎样才能得到我想要的结果? os.system 行是在将“Islem baslatiliyor.#####”写入我的日志文件之后,但它在此之前运行..

我尝试在获取“log_deneme”文件的统计信息之前让我的代码休眠,但它不起作用。之前还在跑..

【问题讨论】:

    标签: python os.system


    【解决方案1】:

    我做到了,伙计们..

    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")
    

    我尝试了这段代码,它成功了。我认为我的问题中的代码没有像我预期的那样工作,因为它位于with open 的选项卡中。

    所以这是我的问题的答案,但不能完全解释问题的根源。这只是我的猜测。

    谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 2015-11-18
      • 2020-09-05
      • 2020-01-27
      • 2021-03-15
      • 2017-10-30
      相关资源
      最近更新 更多