小编chr*_*ley的帖子

在python中调用外部函数

我试图从if语句中的另一个文件返回(执行)一个函数.我已经读过返回语句不起作用,我希望有人知道什么语句可以让我调用外部函数.

该函数创建一个沙箱,但如果存在,我想传递if语句.

这是我使用的一小段代码.

import mks_function  
from mksfunction import mks_create_sandbox  
import sys, os, time  
import os.path  

if not os.path.exists('home/build/test/new_sandbox/project.pj'):
 return mks_create_sandbox()  
else:  
 print pass  
Run Code Online (Sandbox Code Playgroud)

python return

0
推荐指数
1
解决办法
7404
查看次数

Python中月初的日期时间问题

我有一个函数,在一定时间后删除文件.问题是它可以在本月晚些时候使用,但是当我尝试从月初开始删除7天后,它将不会减少到上个月.有谁知道如何让这个工作?下面的代码计算出日期并删除日期.

today = datetime.date.today() # Today's date Binary
todaystr = datetime.date.today().isoformat() # Todays date as a string
minus_seven = today.replace(day=today.day-7).isoformat() # Removes 7 days
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

python

0
推荐指数
1
解决办法
223
查看次数

Python中的字符串操作

我正在尝试写一个命令,但我不想要一条看起来不整洁的长线.我希望将字符串一起添加到命令执行.我在下面有一些代码是电子邮件功能的一部分:

msg = MIMEText("The nightly build status was a SUCCESS\n\nBuild File: http://www.python.org\n\n Build Results File: http://10.51.54.57/sandboxes/", project, "\n")
Run Code Online (Sandbox Code Playgroud)

这显示了一行,我希望有更好的方法来做到这一点.我尝试了下面的代码,但它不起作用.

msg = MIMEText("The nightly build status was a SUCCESS\n\nBuild File: ")
msg += MIMEText("http://www.python.org\n\n Build Results File: ")
msg += MIMEText("http://10.51.54.57/sandboxes/", project, "\n")
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

我尝试过以下代码,但得到:

msg = MIMEText("""The nightly build status was a SUCCESS\n\n
   Build File: """,  
   build_file, """
   \n\n 
   Build Results File: """, 
   build_file, """
   \n\n
   Sandbox Folder:""", 
   sandbox, """ 
   \n\n
   Antibrick File: """,
   antibrick, "\n\n")
Run Code Online (Sandbox Code Playgroud)

现在我收到消息:

Traceback (most recent …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
2
解决办法
439
查看次数

如何将最后50行从一个文件写入另一个文件

我正在创建一个过夜构建的电子邮件响应,我想从结果文件中获取最后50行并将其放入摘要文件中.我所做的代码如下,任何人都可以帮忙吗?

def email_success():

    fp = open(results_file, 'r')

    sum_file = (fp.readlines()[-50:])
    fp.close()

    myfile = open(result_summary,'w')

    myfile.write(sum_file)
    myfile.close()
Run Code Online (Sandbox Code Playgroud)

我在尝试此代码时收到以下错误消息:

Traceback (most recent call last):
  File "email_success.py", line 76, in <module>
    if __name__ == '__main__': myObject = email_success()
  File "email_success.py", line 45, in email_success
    myfile = open(result_summary,'w')
TypeError: coercing to Unicode: need string or buffer, tuple found
Run Code Online (Sandbox Code Playgroud)

谢谢

结果摘要是存储地址的变量.

result_summary = (t, 'results_summary.txt')
Run Code Online (Sandbox Code Playgroud)

抱歉犯了一个愚蠢的错误,我忘了添加os.path.join

result_summary = os.path.join(t, 'results_summary.txt')
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助

@alok这是一个目录地址,我忘了添加os.join使其成为一个字符串.这是造成错误的原因

python

0
推荐指数
1
解决办法
1404
查看次数

如何将列表的值放入字符串中

我试图将列表中的几个值放入一个字符串中.我的代码如下:

ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(ID)
Run Code Online (Sandbox Code Playgroud)

要么

print (r'(ID\s*=\s*)(\S+)').format(ID)
Run Code Online (Sandbox Code Playgroud)

这不起作用.有谁知道我哪里出错了.第二行中的代码打印出列表:

[0, 1, 2]
Run Code Online (Sandbox Code Playgroud)

第一行说:

File "tset.py", line 39, in b
    print 'ID {0}, {1}, and {2}.'.format(ID)
IndexError: tuple index out of range
Run Code Online (Sandbox Code Playgroud)

谢谢

python string-formatting

0
推荐指数
1
解决办法
587
查看次数

从另一个函数Python调用变量

我试图调用一个变量用于另一个函数.该变量仅在另一个函数中,并未声明为全局变量.有谁知道如何调用其他变量.下面的代码显示了正在使用的'retval'变量,但它在另一个函数中声明.

def email_results():

    if make.retval > 0:
        os.system('python email_success.py')
    else: 
        os.system('python email_failure.py')

if __name__ == '__main__': myObject = email_results()
Run Code Online (Sandbox Code Playgroud)

谢谢

我声明的函数是:

def make():

    if os.path.exists(t):

        command = "export ROOTDIR="+rootDir+"; "
        command += "export PROJECT="+project+"; "
        command += "export BUILD_DIR=$ROOTDIR/$PROJECT/basebuild; "
        command += "export AD_EXEC_DIR=$BUILD_DIR/output_dev; "
        command += "export BLDTARGET=MVArm9; "
        command += "export PROFILE=release; "
        command += "cd $ROOTDIR/$PROJECT; "
        command += "make > "+logFileName+" 2>&1"

        print "The command that I will be executing is:"
        print command

    #executing make command …
Run Code Online (Sandbox Code Playgroud)

python

-7
推荐指数
1
解决办法
4523
查看次数

标签 统计

python ×6

return ×1

string-formatting ×1