我正在尝试查找目录路径中的文件列表,这些文件是在过去的“n' 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
由于我使用的是一个简单的查找命令(带格式化和排序),我是否需要确保明确地杀死子进程?
我使用'发现'命令是: 我需要一个过去创建的文件列表' 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
答案 0 :(得分:0)
根据您的代码,"唯一不起作用"是死链接导致它抛出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
个时间戳生成结果