在Windows 7中找不到的文件,但在Windows 10中起作用

发布于 2025-01-30 14:45:57 字数 2500 浏览 2 评论 0原文

我写了一个在Windows 10上使用的Python脚本,但是当我在Windows 7上运行相同的Python版本(3.8)时,它找不到我的输入文件:

这是代码:

# Get list of command line arguments, remove first argument
argumentList = sys.argv[1:]

# Options
options = "hai:s:"

# Long options
longOptions = ["Help", "project", "Input", "Sqo"]


try:
    # Parsing argument
    arguments, values = getopt.getopt(argumentList, options, longOptions)
    print("The arguments are : ", arguments)
    print("The values are", values)
    # checking each argument
    for currentArgument, currentValue in arguments:

        if currentArgument in ("-h", "--Help"):
            print("Convert Polyspace findings from JSON file to console in Visual Studio ccompliant format")
            print("Options:")
            print("  -i, --Input: JSON input file")
            print("  -s, --Sqo: SQO definition file")
            print("  -a, --project: Show results for all rules, even the ones not mandatory to fix")
            print("  -h, --Help : This help")

        elif currentArgument in ("-i", "--Input"):
            inputFile = currentValue
            print(inputFile)

        elif currentArgument in ("-s", "--Sqo"):
            sqoFile = currentValue
            print(sqoFile)

        elif currentArgument in ("-a", "--All"):
            product = currentValue
            print(product)

except getopt.error as err:
    # output error, and return with an error code
    print(str(err))
# read JSON data generated by Polyspace
try:
    with open(inputFile, "r") as readFile:
        data = json.load(readFile)
        sqoLevel = int(sqoRoot.get('level'))
...
except FileNotFoundError:
    print("Error: File not found " + inputFile)
    sys.exit(1)

I在CMD行中运行:

python PolyspaceReportGenerator.py -s polyspaceSqo.xml -i polyspaceResults.json -a XXX

在Windows 10中,一切都很好。 在Windows 7中,我会收到以下错误:

    Error: File not found .\polyspaceResults.json

我什至尝试创建一个绝对路径:

    elif currentArgument in ("-i", "--Input"):
        inputFile = os.path.join("D:\\","Polyspace",currentValue)
        #inputFile = os.path.abspath(inputFile)
        print("The absolute path : ", inputFile)

但是仍然有相同的错误:

The absolute path :  D:\Polyspace\polyspaceResults.json
Error: File not found D:\Polyspace\polyspaceResults.json

I have written a Python script which is working on my Windows 10, but when I run the same code on Windows 7 with the same Python version (3.8), it doesn't find my input file:

Here is the piece of code:

# Get list of command line arguments, remove first argument
argumentList = sys.argv[1:]

# Options
options = "hai:s:"

# Long options
longOptions = ["Help", "project", "Input", "Sqo"]


try:
    # Parsing argument
    arguments, values = getopt.getopt(argumentList, options, longOptions)
    print("The arguments are : ", arguments)
    print("The values are", values)
    # checking each argument
    for currentArgument, currentValue in arguments:

        if currentArgument in ("-h", "--Help"):
            print("Convert Polyspace findings from JSON file to console in Visual Studio ccompliant format")
            print("Options:")
            print("  -i, --Input: JSON input file")
            print("  -s, --Sqo: SQO definition file")
            print("  -a, --project: Show results for all rules, even the ones not mandatory to fix")
            print("  -h, --Help : This help")

        elif currentArgument in ("-i", "--Input"):
            inputFile = currentValue
            print(inputFile)

        elif currentArgument in ("-s", "--Sqo"):
            sqoFile = currentValue
            print(sqoFile)

        elif currentArgument in ("-a", "--All"):
            product = currentValue
            print(product)

except getopt.error as err:
    # output error, and return with an error code
    print(str(err))
# read JSON data generated by Polyspace
try:
    with open(inputFile, "r") as readFile:
        data = json.load(readFile)
        sqoLevel = int(sqoRoot.get('level'))
...
except FileNotFoundError:
    print("Error: File not found " + inputFile)
    sys.exit(1)

I run in cmd line:

python PolyspaceReportGenerator.py -s polyspaceSqo.xml -i polyspaceResults.json -a XXX

in windows 10, everything is fine.
in windows 7, I get the following error:

    Error: File not found .\polyspaceResults.json

I even tried to create an absolute path:

    elif currentArgument in ("-i", "--Input"):
        inputFile = os.path.join("D:\\","Polyspace",currentValue)
        #inputFile = os.path.abspath(inputFile)
        print("The absolute path : ", inputFile)

But is still get the same error:

The absolute path :  D:\Polyspace\polyspaceResults.json
Error: File not found D:\Polyspace\polyspaceResults.json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文