1

Good day! I am wondering if there is any way in which one can allow different programming languages to access a csv at the same time. I am using c# to get live stock market data, and then Python does calculations on this data where it then returns the data to the csv file to be read by c# again, it works if I use multiple steps i.e. collect historical data predict the historical data and read the historical data, but when I try to do this in one step(live) I get the following error.

[Errno 13] Permission denied: 'CurrencyPair-Minute.csv'

I think this is the result of the file being used by the c# program. Which I opened with the following parameters

File.Open(fiName, FileMode.Open, FileAccess.ReadWrite, FileShare.Write);

I only close the data streams when the program stops and to the streams are continually open for reading and writing in the c# program. If I close the stream while the file is not being read or written the error I get is

Crashed in (MethodName) with ArgumentException: Stream was not readable.

Also, this will not work since the Python program continually needs to check the file for updates.

Any help would be appreciated

Thank you

Gugu72
  • 2,052
  • 13
  • 35
  • If you only want to read, you should specify `FileAccess.Read` and `FileShare.ReadWrite` – Klaus Gütter Dec 21 '20 at 06:06
  • Write to a temp file. Once writing is complete, move it to a new filename which the other process can read from. Thus there is an explicit write step then read step - not both happening at once. – mjwills Dec 21 '20 at 06:20

1 Answers1

1

You should be able to run the python script from your C# file after fetching the data and writing to the csv and closing the csv file in C#. See here: How do I run a Python script from C#?.

The flow would be something like this:

C#

  • Fetch data
  • Write to csv
  • Close file
  • Call python script

Python

  • Do calculation
  • Write to file
  • Close file
  • Exit
Ishan
  • 859
  • 5
  • 9
  • Thank you this makes a lot of sense and thank you for the resource you provided! – Leon Von Moltke Dec 21 '20 at 06:39
  • I am not getting any output back from the python program even though it is running do you perhaps know why this might be? I have implemented the code exactly as demonstrated. – Leon Von Moltke Dec 21 '20 at 15:59
  • Not sure found this comment in the accepted answer's comment section "Add a line of `Console.ReadLine();` after `Console.Write(result);` and you should be able to see the result because it is waiting for any key press to exit" – Ishan Dec 21 '20 at 16:10
  • I tried this but it does not work, Alternatively I tried process.WaitForExit(); where I added this as the last line in the nested "using" call, which did not work either. – Leon Von Moltke Dec 21 '20 at 16:41
  • you could ask a separate question and link it here if someone needs it. One thing you could try is redirecting the output for python to a file. `start.Arguments = string.Format("{0} {1} > output.txt 2>&1", cmd, args);`, redirects stdout and stderr to the file output.txt – Ishan Dec 21 '20 at 17:03
  • I cannot find this file I have tried searching my entire computer after running the program and it does not seem to exist. Is this supposed to be in the directory of the python program. I have created a new question https://stackoverflow.com/questions/65390092/run-python-code-from-c-sharp-does-not-return-a-value – Leon Von Moltke Dec 21 '20 at 18:32
  • I have set the redirect output to false and I have written the python code to write to a csv file, trying to see if the output is not being sent or if it is not being received and it seems as though the output is not being sent by the python script. I used the following code. – Leon Von Moltke Dec 21 '20 at 20:27
  • ```ProcessStartInfo start = new ProcessStartInfo(); start.FileName = "Path\\To\\python.exe"; start.Arguments = "Path\\To\\\\Thing.py"; start.UseShellExecute = false; start.RedirectStandardOutput = false; using (Process process = Process.Start(start)) { }``` – Leon Von Moltke Dec 21 '20 at 20:31