首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python os.path中的变量

python os.path中的变量
EN

Stack Overflow用户
提问于 2009-12-21 19:10:35
回答 4查看 8.6K关注 0票数 1

我是python的新手,我正在尝试创建一个程序,创建一个带有今天日期的目录,在该目录中创建一个沙箱,并在沙箱中运行make文件。我在获取要在os.path行中提取的变量时遇到了问题。代码如下:

代码语言:javascript
复制
#!/usr/bin/python  
import mks_function  
from mks_function import mks_create_sandbox  
import sys, os, time, datetime  
import os.path  

today = datetime.date.today()  # get today's date as a datetime type  

todaystr = today.isoformat()   # get string representation: YYYY-MM-DD  
                           # from a datetime type.  

if not os.path.exists('/home/build/test/sandboxes/'+todaystr):  
 os.mkdir(todaystr)  
else:  
 pass  

if not os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/project.pj'):  
 mks_create_sandbox()  
else:  
 pass  

if os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/Makefile'):  
 os.system("make >make_results.txt 2>&1")  

感谢您的帮助,谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-12-21 19:25:59

以下是几个注意事项:

代码语言:javascript
复制
#!/usr/bin/env python  
# import mks_function .. you won't need this ...

from mks_function import mks_create_sandbox  
import os, datetime  

# import time, sys .. these aren't used in this snippet 
# import os.path .. just refer to os.path, since os is already imported

# get today's date as a datetime type  
todaystr = datetime.date.today().isoformat()  

# .. use os.path.join()
if not os.path.exists(os.path.join('/home/build/test/sandboxes/', todaystr)):  
    os.mkdir(os.path.join('/home/build/test/sandboxes/', todaystr))  
# .. 'else: pass' is unnecessary

if not os.path.exists(os.path.join(
    '/home/build/test/sandboxes/', todaystr, '/new_sandbox/project.pj')):  

    # i'm not seen, that the sandbox is created in the right directory here
    # maybe you should change the working directory via ..
    # os.chdir(os.path.join('/home/build/test/sandboxes/', todaystr))
    mks_create_sandbox()  

if os.path.exists(os.path.join(
    '/home/build/test/sandboxes/', todaystr, '/new_sandbox/Makefile')):  

    # .. change to the right directory
    os.chdir(os.path.join(
        '/home/build/test/sandboxes/', todaystr, '/new_sandbox/'))

    os.system("make > make_results.txt 2>&1")  
票数 3
EN

Stack Overflow用户

发布于 2009-12-21 19:21:37

我认为你想要改变一些事情:

代码语言:javascript
复制
def makeSandbox():
  sbdir = os.path.join('/home/build/test/sandboxes/',todaystr)
  if not os.path.exists(sbdir):  
    os.mkdir(sbdir)  # <- fully qualified path
  else:  
    pass

我真的看不出需要选择哪些变量,对我来说似乎没问题。

票数 1
EN

Stack Overflow用户

发布于 2009-12-21 19:24:44

请在调用make之前尝试添加chdir代码

代码语言:javascript
复制
if os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/Makefile'):
 os.chdir('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/')
 os.system("make >make_results.txt 2>&1")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1939496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档