0

Running below code in Azure Blobstorage concurrently is throwing OS Error 22:Invalid argument pointing to f.close()

Is using Close() when using with open() creating OS error 22 issues? Understand that Close() is not required but want to understand the root cause of OS error 22

*with open(openfilepath,"w+") as f:
     f.write(writeFile )
     f.close()*
John_Che
  • 1
  • 4
  • Can you please provide the complete filepath ? – kavyaS Aug 11 '21 at 12:04
  • it is absolute path with the mount location from Azure databricks. It is happening a random fashion, when run concurrently (on different location not the same) – John_Che Aug 11 '21 at 13:59

2 Answers2

1

Check the below possibilities and try.

1

If f.close() to be used you may try this by not using with

f = open(file_name, 'w+')  # open file in write mode 

f.write('write content') 

f.close()

BUT
It is good practice to use  with  keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes. once Python exits from the “with” block, the file is automatically closed. 

So Please Remove f.close() and try.

with open(file_name, 'w+') as f :       

f.write('write content') 

Refer this for more info.

2

If above doen’t work check the filepath : It might be due to some invalid characters present in the file path name: It should not contain few special characters. See if file path is for example : "dbfs:/mnt/data/output/file_name.xlsx" Check if “/” is there before dbfs ( say /dbfs:/mnt/…).Try Removing if present.

NOTE: ``r+'': Open for reading and writing. The stream is positioned at the beginning of the file.

``w+'': Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

``a+'' : Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subse- quent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar.

So try using other modes like r+ if w+ is not mandatory .See Python documentation

References: 1 , 2 ,3 , 4

kavyaS
  • 8,026
  • 1
  • 7
  • 19
-2

Removing the F.close() fixed the issues when using With

John_Che
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – jahantaila Nov 12 '21 at 16:03
  • Please read [ask] and note well that this is **not a discussion forum**. If your intent is to tell someone else that a specific idea from their answer solved the problem, do not use an answer for this. It is not necessary to say it at all, but if you have something *specific* to say (more than "thanks, that helped"), you may use a comment. – Karl Knechtel Aug 28 '22 at 21:14