python + linux + 不终止子进程问题

python + linux + not killing subprocess issues

我正在尝试找出目录路径中的文件列表,这些文件是在过去 'n' 分钟内创建的,使用 subprocess.popen 和 'find' linux命令。

def Subprocessroutine(n,path):
     argf='find '+str(path)+' -maxdepth 1 -mmin -'+str(n)+' -type f -printf "\n%AD %AT %p" | sort'
     p=subprocess.Popen(argf,stdout=subprocess.PIPE,shell=True)
     soutput,sinput=p.communicate()
     return soutput

因为我使用的是一个简单的查找命令(带有格式化和排序),我是否需要确保明确地终止子进程?

我使用 'find' 命令的原因是: 我需要在过去 'n' 分钟内创建的文件列表,结果应按文件创建排序顺序排列(我无法使用以下代码获得此信息):

for p, ds, fs in os.walk(dirpath):
    for fn in fs:
        filepath = os.path.join(p, fn)
        if os.path.getmtime(filepath) >= n:
            print(datetime.datetime.fromtimestamp(os.path.getmtime(filepath)))

是否有更好的方法来获得结果,而不使用子进程?

我的最终输出应该是这样的:

09/26/1619:41:04.4865673390 /home/Testing/Input/Sep26/HST39_2016-09-26 19:40:03.283121_2016-09-26 19:41:03.283109.csv
09/26/1619:41:04.4875673570 /home/Testing/Input/Sep26/HST40_2016-09-26 19:40:03.283561_2016-09-26 19:41:03.283552.csv
09/26/1619:41:04.4885673750 /home/Testing/Input/Sep26/HST41_2016-09-26 19:40:03.283988_2016-09-26 19:41:03.283980.csv
09/26/1619:41:04.4895673930 /home/Testing/Input/Sep26/HST42_2016-09-26 19:40:03.284408_2016-09-26 19:41:03.284399.csv
09/26/1619:41:04.4905674110 /home/Testing/Input/Sep26/HST43_2016-09-26 19:40:03.284852_2016-09-26 19:41:03.284843.csv
09/26/1619:41:04.4915674290 /home/Testing/Input/Sep26/HST44_2016-09-26 19:40:03.285295_2016-09-26 19:41:03.285288.csv

根据您的代码,唯一 "did not work" 是无效链接导致它抛出 OSError。我已将其修改为以下内容:

#!/usr/bin/env python

import os
import time
import datetime

dirpath = "./"

for p, ds, fs in os.walk(dirpath):
    for fn in fs:
        filepath = os.path.join(p, fn)
        # This catches bad links!
        try:
            if os.path.getmtime(filepath) >= time.time() - 30*60:
                dt_mod = datetime.datetime.fromtimestamp(os.path.getmtime(filepath))
                print("Adding   %s - %s" % (str(dt_mod), filepath))
            else:
                print("Skipping %s - %s" % (str(dt_mod), filepath))
        except OSError as e:
            print("Arrrgggg: %s" % str(e))

请注意 time.time() - 30*60 是当前时间减去 30 分钟,其中 time.time() 为您提供当前时间 float。我在 /tmp 文件夹中 运行...结果是:

$ python ./test.py 
Adding   2016-09-26 19:48:24.913770 - ./test.py
Skipping 2016-09-26 19:48:24.913770 - ./qtsingleapp-homeur-6219-3e8-lockfile
Skipping 2016-09-26 19:48:24.913770 - ./.X0-lock
Skipping 2016-09-26 19:48:24.913770 - ./urban.socket
Skipping 2016-09-26 19:48:24.913770 - ./config-err-yVuRIy
Skipping 2016-09-26 19:48:24.913770 - ./qtsingleapp-homeur-6219-3e8
Adding   2016-09-26 19:19:53.685688 - ./.com.vivaldi.Vivaldi.82Dchr/SingletonSocket
Arrrgggg: [Errno 2] No such file or directory: './.com.vivaldi.Vivaldi.82Dchr/SingletonCookie'
Skipping 2016-09-26 19:19:53.685688 - ./ksocket-urban/klauncherhX3020.slave-socket
Skipping 2016-09-26 19:19:53.685688 - ./ksocket-urban/KSMserver__0
Skipping 2016-09-26 19:19:53.685688 - ./ksocket-urban/kdeinit4__0
Skipping 2016-09-26 19:19:53.685688 - ./skype-3294/DbTemp./temp-vL1IkSGDS19DRPp35idqPr9i
Skipping 2016-09-26 19:19:53.685688 - ./skype-3294/DbTemp./temp-oBEOkqraeZoXoyOrtvgzyxgE
Skipping 2016-09-26 19:19:53.685688 - ./skype-3294/DbTemp./temp-urolswo9ukl5kCzrRAD3epUw
Skipping 2016-09-26 19:19:53.685688 - ./skype-3294/DbTemp./temp-u5xJMdmkGEwV7K1nhqzx9Jg0
Adding   2016-09-26 19:44:48.309759 - ./kde-urban/konsole-Jw4443.history
Adding   2016-09-26 19:48:25.661770 - ./kde-urban/konsole-tX4539.history
Adding   2016-09-26 19:48:25.661770 - ./kde-urban/konsole-SK4539.history
Adding   2016-09-26 19:48:25.661770 - ./kde-urban/konsole-Jt4539.history
Skipping 2016-09-26 19:48:25.661770 - ./kde-urban/xauth-1000-_0
Adding   2016-09-26 19:44:48.309759 - ./kde-urban/konsole-cq4443.history
Adding   2016-09-26 19:44:48.309759 - ./kde-urban/konsole-WK4443.history
Skipping 2016-09-26 19:44:48.309759 - ./gpg-t0jSN1/S.gpg-agent
Skipping 2016-09-26 19:44:48.309759 - ./.X11-unix/X0
Skipping 2016-09-26 19:44:48.309759 - ./.ICE-unix/3044

我没有对结果进行排序或存储,但这很容易做到...

编辑

排序部分就是存储我们喜欢的结果,排序后打印:

#!/usr/bin/env python

import os
import time
import datetime

dirpath = "./"

results = []

for p, ds, fs in os.walk(dirpath):
    for fn in fs:
        filepath = os.path.join(p, fn)
        # This catches bad links!
        try:
            if os.path.getmtime(filepath) >= time.time() - 30*60:
                dt_mod = os.path.getmtime(filepath)
                print("Adding   %s - %s" % (str(dt_mod), filepath))
                results.append((filepath, os.path.getmtime(filepath)))
            else:
                print("Skipping %s - %s" % (str(dt_mod), filepath))
        except OSError as e:
            print("Arrrgggg: %s" % str(e))


results.sort(key=lambda x: x[1])
for s in results:
    print(s)

我删除了 datetime 转换,因为结果只能使用 epoch 时间戳