<i id='ze0qI'><tr id='ze0qI'><dt id='ze0qI'><q id='ze0qI'><span id='ze0qI'><b id='ze0qI'><form id='ze0qI'><ins id='ze0qI'></ins><ul id='ze0qI'></ul><sub id='ze0qI'></sub></form><legend id='ze0qI'></legend><bdo id='ze0qI'><pre id='ze0qI'><center id='ze0qI'></center></pre></bdo></b><th id='ze0qI'></th></span></q></dt></tr></i><div id='ze0qI'><tfoot id='ze0qI'></tfoot><dl id='ze0qI'><fieldset id='ze0qI'></fieldset></dl></div>
    <bdo id='ze0qI'></bdo><ul id='ze0qI'></ul>
    1. <tfoot id='ze0qI'></tfoot>

      1. <small id='ze0qI'></small><noframes id='ze0qI'>

        <legend id='ze0qI'><style id='ze0qI'><dir id='ze0qI'><q id='ze0qI'></q></dir></style></legend>

      2. 将系统命令转换为 python 以进行文件搜索和删除

        Converting a system command to python for file search and delete(将系统命令转换为 python 以进行文件搜索和删除)
        1. <i id='XqODb'><tr id='XqODb'><dt id='XqODb'><q id='XqODb'><span id='XqODb'><b id='XqODb'><form id='XqODb'><ins id='XqODb'></ins><ul id='XqODb'></ul><sub id='XqODb'></sub></form><legend id='XqODb'></legend><bdo id='XqODb'><pre id='XqODb'><center id='XqODb'></center></pre></bdo></b><th id='XqODb'></th></span></q></dt></tr></i><div id='XqODb'><tfoot id='XqODb'></tfoot><dl id='XqODb'><fieldset id='XqODb'></fieldset></dl></div>
          <legend id='XqODb'><style id='XqODb'><dir id='XqODb'><q id='XqODb'></q></dir></style></legend>

            <tbody id='XqODb'></tbody>
          • <bdo id='XqODb'></bdo><ul id='XqODb'></ul>
            <tfoot id='XqODb'></tfoot>

              <small id='XqODb'></small><noframes id='XqODb'>

                  本文介绍了将系统命令转换为 python 以进行文件搜索和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 cron 作业,它使用以下命令根据文件的年龄删除文件:

                  I have a cron job that deletes files based on their age using the command:

                  find /path/to/file/ -type f -mmin +120|xargs -I file rm 'file'
                  

                  但是我想将该命令集成到一个 python 脚本中,该脚本涉及该任务以及其他也在 cron 上运行的东西.

                  However I'd like to integrate the command into a python script which involves the task as well as other things that also run on cron.

                  我知道我可以将命令按原样插入 Python 脚本中,它可能会运行 find,但是我很想知道有一种更以 Python 为中心的方式来完成此任务,以及这可能带来哪些其他好处?

                  I understand I could just plonk the command as it is into the Python script and it would probably run find, however I'm interested to know there a more Python-centric way to accomplish this and what other benefits might that bring?

                  推荐答案

                  我的方法是:

                  import os
                  import time
                  
                  def checkfile(filename):
                      filestats = os.stat(filename) # Gets infromation on file.
                      if time.time() - filestats.st_mtime > 120: # Compares if file modification date is more than 120 less than the current time.
                          os.remove(filename) # Removes file if it needs to be removed.
                  
                  path = '/path/to/folder'
                  
                  dirList = os.listdir(path) # Lists specified directory.
                  for filename in dirList:
                      checkfile(os.path.join(path, filename)) # Runs checkfile function.
                  

                  我测试了它,它没有工作,所以我修复了代码,我可以确认它工作.

                  I tested it, it didn't work, so i fixed the code and i can confirm it works.

                  这篇关于将系统命令转换为 python 以进行文件搜索和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  <small id='8B7pi'></small><noframes id='8B7pi'>

                    • <legend id='8B7pi'><style id='8B7pi'><dir id='8B7pi'><q id='8B7pi'></q></dir></style></legend>

                        <tbody id='8B7pi'></tbody>
                      <i id='8B7pi'><tr id='8B7pi'><dt id='8B7pi'><q id='8B7pi'><span id='8B7pi'><b id='8B7pi'><form id='8B7pi'><ins id='8B7pi'></ins><ul id='8B7pi'></ul><sub id='8B7pi'></sub></form><legend id='8B7pi'></legend><bdo id='8B7pi'><pre id='8B7pi'><center id='8B7pi'></center></pre></bdo></b><th id='8B7pi'></th></span></q></dt></tr></i><div id='8B7pi'><tfoot id='8B7pi'></tfoot><dl id='8B7pi'><fieldset id='8B7pi'></fieldset></dl></div>
                          <bdo id='8B7pi'></bdo><ul id='8B7pi'></ul>
                          <tfoot id='8B7pi'></tfoot>