所以我有一个脚本需要在服务器上以不同的时间间隔做 3 件事。
- 如果下载(使用子进程),则扫描 FTP 以获取 24 小时内更新的文件,
- 扫描位置 1,如果文件存在移动文件并根据文件的来源重命名(使用子进程),
- 扫描目录,如果文件存在,则将文件上传到不同的 FTP(使用子进程)。
除了这个,一切都很好;在第 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"
这是输出:
扫描 /Volumes/VoigtKampff/Temp/_Jonatha/BeeCell/encodeDrop_dir - 时间是:2013-03-19 18:22:25.345483
上传列表:[]
你好
更新:如果我将扫描功能移动到子进程调用的脚本中 - 它可以工作。如果以上述方式使用,我还没有发现为什么它会失败。