2015-11-13 36 views
0

我有一个过程,可以扫描磁带库并查找已过期的介质,因此可以在将磁带发送到非现场保管库之前将其删除并重新使用。 (我们有一些7天的策略,绝不会使其不在现场)。这个过程大约需要20分钟才能运行,所以我不希望它在加载/刷新页面时按需运行。相反,我设置了一个django-cron作业(我知道我可以在Linux cron中完成这项工作,但希望项目尽可能独立)来运行扫描,并在/ tmp中创建一个文件。我已经验证了这一点 - 从今天上午执行的文件存在于/ tmp中。我遇到的问题是,现在我想在我的网页上显示这些过期(暂存)介质的列表,但脚本说它找不到该文件。当创建的文件,我用的是绝对路径“/tmp/scratch.2015-11-13.out”(例如),但这里是我在浏览器中出现错误:Django:没有这样的文件或目录

IOError at/
[Errno 2] No such file or directory: '/tmp/corpscratch.2015-11-13.out' 

我的假设是这是一个“网络根”问题,但我无法弄清楚。我尝试将该文件复制到在django中配置的/ static /和/ media /目录中,甚至在django根目录和项目根目录中,但似乎没有任何效果。当它说'找不到/ tmp/file,它真的在哪里看?

def sample(): 
    """ Just testing """ 
    today = datetime.date.today() #format 2015-11-31 
    inputfile = "/tmp/corpscratch.%s.out" % str(today) 
    with open(inputfile) as fh:   # This is the line reporting the error 
     lines = [line.strip('\n') for line in fh] 
    print(lines) 

print语句用于在外壳的测试(工作,我可以补充),但浏览器提供了一个错误。 和文件确实存在:

$ ls /tmp/corpscratch.2015-11-13.out 
/tmp/corpscratch.2015-11-13.out 

感谢。

编辑:被误认为是不能在python shell中工作的。正在考虑以前的问题。

回答

0

我结束了在其他地方找到这样的:

today = datetime.date.today() #format 2015-11-31 
inputfilename = "tmp/corpscratch.%s.out" % str(today) 
inputfile = os.path.join(settings.PROJECT_ROOT, inputfilename) 

,用含有如下settings.py:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 

完全解决了我的问题。

0

使用这个代替:

today = datetime.datetime.today().date() 
inputfile = "/tmp/corpscratch.%s.out" % str(today) 

或者:

today = datetime.datetime.today().strftime('%Y-%m-%d') 
inputfile = "/tmp/corpscratch.%s.out" % today # No need to use str() 

看到区别:

>>> str(datetime.datetime.today().date()) 
'2015-11-13' 

>>> str(datetime.datetime.today()) 
'2015-11-13 15:56:19.578569' 
+0

这不能解决我的问题。我使用的是datetime.date,而不是datetime.datetime,所以生成的字符串匹配。 (2015年11月13日)。 –

相关问题