IOError: [Errno 22] invalid mode ('w') or filename: "C:/Users/...../file.csv"
<p>I'm running a simulation in which I'm reading and writing multiple files (both .txt or .csv) at each timestep in order to communicate with external programs in C#. </p>

<p>Every so often in the simulation, I have the error: 
IOError: [Errno 22] invalid mode ('w') or filename:"C:/Users/..../filename.csv"</p>

<p>It doesn't happen every time I run it, and if I run each line manually it sometimes works just fine but sometimes not. </p>

<p>I'm writing the .csv's like this:    </p>

<pre><code>path=r'C:/Users/..../filename.csv'
datatowrite.to_csv(path, index=False)
</code></pre>

<p>and the .txt files like this:</p>

<pre><code>    path=r'C:/Users/.../Filename.txt'
    np.savetxt(path, datatowrite)
</code></pre>

<p>I looked at similar questions but most have to do with use of backslashes and colons so I use raw strings and forward slashes throughout my code and I turn the string into a variable "path" before each time I save or access a file. When I run the simulation it will work sometimes, and other times I get an error:</p>

<pre><code>[Errno 22] invalid mode ('w') or filename: 'C:/Users/.../Filename.txt'
</code></pre>

<p>I should note that I am writing and saving files on Dropbox - could this be the issue? Perhaps the files are not syncing fast enough before the next line tries to access them. Regardless, I don't understand why it works sometimes and doesn't work other times. Any suggestions? </p>