Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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
C# 在Python的“with”子句中写入并随后由C进程读取的文件被锁定。有什么可能的原因吗?_C#_Python_Xml - Fatal编程技术网

C# 在Python的“with”子句中写入并随后由C进程读取的文件被锁定。有什么可能的原因吗?

C# 在Python的“with”子句中写入并随后由C进程读取的文件被锁定。有什么可能的原因吗?,c#,python,xml,C#,Python,Xml,使用以下代码在Python进程中创建并写入新的XML文件: with open(filepath, 'w') as f: f.write(inXmlStr) 随后,Python进程将启动一个C进程: with open(self.outputLogPath, 'w') as f: self.csharpProcess = subprocess.Popen(processOpenCmd, stdout=f) 此C进程使用XmlDocument类读取新创建的XML文件: 但是,有

使用以下代码在Python进程中创建并写入新的XML文件:

with open(filepath, 'w') as f:
    f.write(inXmlStr)
随后,Python进程将启动一个C进程:

with open(self.outputLogPath, 'w') as f:
    self.csharpProcess = subprocess.Popen(processOpenCmd, stdout=f)
此C进程使用XmlDocument类读取新创建的XML文件:

但是,有时会发现文件被锁定,C进程无法读取。通过使用发现,锁定XML文件的进程实际上是最初创建并写入该文件的Python进程。这是出乎意料的,因为Python的with子句应该正确地关闭所有打开的文件。因此,我怀疑这是一个Python错误

我正在使用Python2.7和.NETFramework4开发Windows7

C进程的异常stacktrace如下所示:

System.IO.IOException: The process cannot access the file 'C:\...\...\channel.xml' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(String filename)
at ChannelXmlRun(String xmlFile)

你知道为什么会这样吗?也许这是一个已知的Python错误,您可以告诉我

当C程序遇到锁定的文件时,您确定Python程序已退出with block吗?@zmbq是的,应该是这样,因为Python程序的流是线性的,Popen语句位于with block之后。如果停止with并显式关闭该文件会怎么样?是的,我已经尝试过了。但在C程序中有时仍然会遇到同样的异常这不是问题的解决方案,但可能是问题的解决方案:如果将XML传递到C进程的stdin中会怎么样?
System.IO.IOException: The process cannot access the file 'C:\...\...\channel.xml' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(String filename)
at ChannelXmlRun(String xmlFile)