Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中同时使用While+if语句时出现问题_Python_Subprocess - Fatal编程技术网

在Python中同时使用While+if语句时出现问题

在Python中同时使用While+if语句时出现问题,python,subprocess,Python,Subprocess,有人能检查一下下面的代码吗,因为我在python中同时使用while和If语句并调用了一个以上的python代码,但它正在执行除调用其他python代码之外的所有代码 import os import psycopg2 import os.path import subprocess import sys if os.path.isfile("Z:\\xyz\\New.xlsx"): while True: conn = psycopg2.connect(databas

有人能检查一下下面的代码吗,因为我在python中同时使用while和If语句并调用了一个以上的python代码,但它正在执行除调用其他python代码之外的所有代码

import os
import psycopg2
import os.path
import subprocess
import sys

if os.path.isfile("Z:\\xyz\\New.xlsx"):
    while True:
        conn = psycopg2.connect(database="gp_bdl", user="adwed1", password="***", host="bdlgp12", port="6200")
        cur = conn.cursor()
        print ("Opened database successfully")    
        try:
            cur.execute('''select function1();''')
            conn.commit()
            conn.close()

        except psycopg2.OperationalError:
            continue
        break;
        if os.path.isfile("Z:\\xyz\\second.xlsx"):
         print("Next has started")
         subprocess.call([sys.executable,'Next Python.py'])
    else:
         print("Nothing")
else:
   print("No Process")*
你的break语句之后的一切都是死代码。如果您的尝试引发错误,continue语句将启动一个与pass不同的新迭代,这将使流程从该点开始

但若它并没有引发异常,那个么你们就突破了时间,所以你们的第一个if是不可到达的

import os
import psycopg2
import os.path
import subprocess
import sys

if os.path.isfile("Z:\\xyz\\New.xlsx"):
    while True:
        conn = psycopg2.connect(database="gp_bdl", user="adwed1", password="***", host="bdlgp12", port="6200")
        cur = conn.cursor()
        print ("Opened database successfully")    
        try:
            cur.execute('''select function1();''')
            conn.commit()
            conn.close()

        except psycopg2.OperationalError:
            continue

        if os.path.isfile("Z:\\xyz\\second.xlsx"):
            print("Next has started")
            subprocess.call([sys.executable,'Next Python.py'])
        else:
            print("Nothing")

        break
else:
    print("No Process")*

就像我的评论所说的那样,循环中break语句的所有内容都不会执行

必须将中断发送到异常分支中的另一个位置。否则,代码部分:

if os.path.isfile("Z:\\xyz\\second.xlsx"):
    print("Next has started")
    subprocess.call([sys.executable,'Next Python.py'])

将永远不会执行,因此不会加载其他文件。

为什么代码不工作?答复:

打破整个循环,移除它! 用pass替换continue 在printNo过程中移除那个微小的** Python中没有分号! 固定代码:

import os
import psycopg2
import os.path
import subprocess
import sys

if os.path.isfile("Z:\\xyz\\New.xlsx"):
    while True:
        conn = psycopg2.connect(database="gp_bdl", user="adwed1",password="***", host="bdlgp12", port="6200")
        cur = conn.cursor()
        print ("Opened database successfully")    
        try:
            cur.execute('''select function1();''')
            conn.commit()
            conn.close()

        except psycopg2.OperationalError:
            continue
        break
        if os.path.isfile("Z:\\xyz\\second.xlsx"):
            print("Next has started")
            subprocess.call([sys.executable,'Next Python.py'])
    else:
         print("Nothing")
else:
   print("No Process")

请更具体一点-你的问题是什么还不清楚。另外,第一条else语句未与if对齐-请确保代码缩进正确。当我运行上述代码时,它没有执行第二条if语句,inturn无法执行subprocess@UnholySheep我不知道这是否是OP真正想要的,但是,while/else是有效的Python语法。这是因为您突破了代码,这意味着您的if语句需要在break语句之前出现