首页 > 解决方案 > 当我从 c# 运行它时,为什么 python 无法识别文本文件?

问题描述

有问题的代码:

```
def loadNumbers(file="ClientNumbers.txt"):
    numbersFile = open(file)
    numbers = numbersFile.readlines()
    return numbers
```

从我的 python IDE 运行此代码时,它工作正常。但是,当我使用 C# 中的系统诊断程序运行 python 脚本时,我收到 ClientNumbers.txt 不是现有文件或目录的错误。

C#代码:

 ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:\Users\francois\AppData\Local\Programs\Python\Python38-32\python.exe";
        start.Arguments = @"..\..\python\sender.py";
        start.UseShellExecute = false;
        start.CreateNoWindow = true;
        start.RedirectStandardOutput = true;
        start.RedirectStandardError = true;
        start.LoadUserProfile = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string stderr = process.StandardError.ReadToEnd();
                string result = reader.ReadToEnd(); 
                Console.WriteLine("success from c#");
                Console.WriteLine(result);
                Console.WriteLine(stderr);
            }
        }

这背后有什么原因吗?

我尝试移动文本文件并相应地调整路径,但没有成功。

C# 应用程序和 python 脚本位于一个文件夹内的不同子文件夹中。

调用 loadNumbers 方法并将其分配给变量: number= loadNumbers()

这是程序停止并给我文件未找到错误的地方

标签: pythonc#text-files

解决方案


推荐阅读