python扫描文件目录

时间:2013-03-19 18:59:22

标签: python

所以我有一个脚本需要在服务器上以不同的间隔做3件事。

  1. 如果下载(使用子流程)
  2. ,则扫描FTP以查找24小时以上的文件
  3. 扫描位置1,如果存在文件,则根据文件的来源移动文件并重命名(使用子进程),
  4. 扫描目录,如果文件存在则将文件上传到不同的FTP(使用子进程)。
  5. 除此之外一切都很好;在第3次扫描(与第2次扫描相同)由于某种原因,脚本不想超越“for root,dirs,files in”

    import sys
    import os
    import subprocess 
    import time
    import datetime
    from datetime import datetime as dt
    
    # Local Directories 
    N_dir_dest = '/Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/N_dest' # Directory where the      NICK source video files needs to be downloaded to
    M_dir_dest = '/Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/M_dest' # Directory where the     NICK source video files needs to be downloaded to
    encode_dest1 = '/Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/encode_dest1' # Encoding Directory where source videos are moved to after being downloaded and renamed
    encodeDrop_dir = '/Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/encodeDrop_dir' # Files ready for uploading to BeeCell FTP 
    
    #Script Starts
    
    while True:
            time.sleep(10)
            if dt.now().hour in range(20, 23):
                print "Starting to Scan UK FTP for source files - time is: %s" % dt.now()
                thedownloadprocess = subprocess.Popen([sys.executable, "/Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/BeeCell_FTP_incoming.py"])
                thedownloadprocess.communicate()
                time.sleep(3600)
    
    
            elif dt.now().minute in range(01, 15) or dt.now().minute in range(31, 45):
                time.sleep(10)
                print "Scanning %s and %s - time is: %s" % (N_dir_dest, M_dir_dest, dt.now())
                for root, dirs, files in os.walk(N_dir_dest) or os.walk(M_dir_dest):
                    for file in files:
                        path_to_file = os.path.join(root, file)
                        if os.path.isfile(path_to_file) and file.endswith(('.mov', '.mpg', '.mp4')):
                            print "file: %s found - starting rename process" % path_to_file
                            renameprocess = subprocess.Popen([sys.executable, "/Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/BeeCell_rname_files.py"])
                            renameprocess.communicate()
                            print "Finished Rename and encode - sleeping for 10 secs- time is: %s" % dt.now()
                            time.sleep(10)
                        else:
                            print "No files in %s or %s" % (N_dir_dest, M_dir_dest)
                            time.sleep(20) 
    
    
    
            elif dt.now().minute in range(16, 30) or dt.now().minute in range(46, 00): # checking every 120 seconds in the minute range 20 - 59
                time.sleep(10)
                print "scanning %s - time is: %s" % (encodeDrop_dir, dt.now())
                for root, dirs, files in os.walk(encodeDrop_dir):
                    for file in files:
                        path_to_file = os.path.join(root, file)
                        if os.path.isfile(path_to_file) and file.endswith(('.mov', '.mpg', '.mp4')):
                            print "file: %s will be uploaded"
                            Nuploadprocess = subprocess.Popen([sys.executable, "/Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/BeeCell_FTPupload.py"]) 
                            Nuploadprocess.communicate()
                            print "Finished Upload - sleeping for 10 secs- time is: %s" % dt.now()
                            time.sleep(10)
                        else:
                            print "No files in %s" % encodeDrop_dir
                            time.sleep(10)
    
            else:
                print "Relaxing"
    

    当我运行脚本时,第一个“elif”运行正常并启动子进程,如果在目录中找到一个文件 - 但在第二个“elif”它只是循环这个:

    elif dt.now().minute in range(16, 30) or dt.now().minute in range(46, 00): # checking every 120 seconds in the minute range 20 - 59
                time.sleep(10)
                print "scanning %s - time is: %s" % (encodeDrop_dir, dt.now())
                for root, dirs, files in os.walk(encodeDrop_dir):
    

    循环继续,直到dt.now()范围继续。我看不出第一个“elif”和第二个“elif”之间的代码有什么不同 - 我愿意在过去几天破坏我的大脑之后听到任何建议,试图弄清楚出了什么问题! ??

    确定添加了打印功能:

        print "scanning %s - time is: %s" % (encodeDrop_dir, dt.now())
                print "Upload List: %s" % E_dir_oslist
                for root, dirs, files in os.walk(encodeDrop_dir):
                    print "hello"
                    for file in files:
                        print "hello 1"   
    

    这是输出:

    scanning / Volumes / VoigtKampff / Temp / _Jonatha / BeeCell / encodeDrop_dir - 时间是:2013-03-19 18:22:25.345483

    上传列表:[]

    您好

    更新:如果我将扫描功能移动到子进程调用的脚本中 - 它可以工作。我还没有发现如果以上述方式使用它会失败的原因。

0 个答案:

没有答案