Tag Archives: Python OSError: [Errno 22] Invalid argument:

Python OSError: [errno 22] invalid argument: [How to Solve]

When executing the report on the web page to generate a word document, the following two errors occur:

1.ValueError: Invalid format string error 
2.Python OSError: [Errno 22] Invalid argument:report name xxxx

Through the query, it is found that these two problems are caused by this sentence:

file_name = 'REPORTS_%s.docx'%time.strftime('%Y-%m-%d %H:%M:%s') 
open with(file_name,'rb')as fp:
  ...

Solution to 1: if you want to change the small s in% s to the large s, you won’t report an error.

Solution to2: there is a problem in time acquisition. Colons cannot be used between hours, minutes and seconds!!!, Just separate them with an underline. Amend to read:

file_name = 'REPORTS_%s.docx'%time.strftime('%Y-%m-%d %H_%M_%S')

As for the solution of problem 2, I checked the relevant information and found that the following special characters [?*: “& lt; & gt;/|] cannot appear in the file name under windows. When using open with to read and write a file, the file name cannot be

Contains the above keywords. It can be solved by changing to other characters.

No error will be reported after the above problems are solved

Python oserror: [errno 22] invalid argument: emergence and solution

 

 1 if __name__ == '__main__':
 2     startime = time.strftime('%H:%M:%S')
 3     print("Start time is: %s" % startime)
 4 # Test path
 5 test_dir = '. /t/test_case'
 6 # Report path
 7 report_dir = '. /t/report/'
 8 
 9 now = time.strftime('%Y-%m-%d_%H:%M:%S')
10 # Create the full report file
11     filename = report_dir + now + '_report.html'
12     fp = open(filename,'wb')

See!! Look at the ninth line, there is a problem with the acquisition time of now!!! Can’t use colon: can’t use colon: can’t use colon: can’t use colon: important things say three times

It is amended as follows:

1 if __name__ == '__main__':
 2     startime = time.strftime('%H:%M:%S')
 3     print("Start time is: %s" % startime)
 4 # Test path
 5 test_dir = '. /t/test_case'
 6 # Report path
 7 report_dir = '. /t/report/'
 8 
 9 now = time.strftime('%Y-%m-%d_%H_%M_%S')
10 # Create the full report file
11     filename = report_dir + now + '_report.html'
12     fp = open(filename,'wb')