patent download, PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

Question:

I am trying to run this piece of code (https://github.com/ryanwhalen/patentsview_data_download/blob/master/patentsview_download.py), and got the following error:

Traceback (most recent call last):
  File "...patentsview_data_download-master221113patentsview_download_221113.py", line 285, in <module>
    download_and_parse_tsv(url)                                          
  File "...patentsview_data_download-master221113patentsview_download_221113.py", line 232, in download_and_parse_tsv
    write_to_db(tsv_name, tablename)                                  
  File "...patentsview_data_download-master221113patentsview_download_221113.py", line 195, in write_to_db
    os.remove(os.getcwd()+'/'+cleaned_file)                       
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '...patentsview_data_download-master\221113/g_applicant_not_disambiguated.tsv2'

Can someone let me know how I could fix the error? Thanks.

I am expecting to run this piece of code and download the patent datasets.

Asked By: James Zheng

||

Answers:

It’s because at line 142 the intermediate file is opened but never closed. This is the line:

infile = open(os.getcwd()+'/'+cleaned_file, 'r', encoding = 'utf-8')

I’d suggest to rewrite it like this:

with open(os.getcwd()+'/'+cleaned_file, 'r', encoding = 'utf-8') as infile:
    # here goes the rest of file processing
Answered By: kol
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.