1

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
  • 41
  • 4

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

kavyasaraboju-MT
  • 3,183
  • 1
  • 4
  • 11
-1

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

John_Che
  • 41
  • 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