Use of open python OSError: [Errno 22] Invalid argument error

In these days when news of a writing spider, met OSError: [Errno 22] Invalid argument this error, distress two days, to no avail. Later, through to ask seniors, we found that was open the file name contains a number of sensitive character system, the results on the error. See a simple code:

1 title = ' see me as the news * title bar. ' 
2 contetn = ' put me as it is the content of the news! ! ! ' 
. 3 with Open (title + ' .txt ' , ' A ' , encoding = ' UTF-. 8 ' ) AS F:
 . 4      f.write (contetn)
 . 5  
. 6  # given as follows: 
. 7  # Traceback (MOST Recent Last Call): 
. 8  #    File "E: / Py Project / my_spider / east_money / test2.py", Line. 3, in <Module1> 
. 9  #      with Open (title + 'TXT.', 'A', encoding = 'UTF-. 8') AS f:
 OSError: [Errno 22] Invalid argument: 'see me as the news * title bar. .TXT'

As can be seen, given the contents of the third line, an error message is invalid parameter, look at the third line of code, guess should be a fault in title inside the content, try inside the '*' removed, the result of an error no. The original, windows down when the file name can not contain special characters, should not conflict with the system, online search, a total of nine sensitive characters, namely *:?. "<> \ / | These characters in the system They have a special role

Found out why, try to solve.

1: replace method

First, try using replace () function to replace the '*' character , a successful method.

title = '* to me as news of the title. '.Replace (' * ',' ')

But clever little friends might have thought, that if the emergence of another eight characters, it has not given a thing. Yes, so that another method may be used.

2: translate method

= intab "* / \ |:?.> <" 
outtab = "" 
trantab = str.maketrans (intab, outtab) 
?. title = 'put me | into a new smell \ * of the title. '.translate (trantab) 
contetn =' put me as the contents of the news! ! ! ' 
With Open (title +'. TXT ',' A ', encoding =' UTF-. 8 ') AS F: 
    f.write (contetn)

The method used here two strings, the first one is maketrans, the second is the translate, maketrans () method for creating a conversion table character mapping, accepts two parameters, the first parameter is a string that represents the required conversion of character, and the second parameter is the string that represents the goal conversions.

translate () method in accordance with the table given parameter table (256 characters) conversion character string, table - translation tables, the translation table is converted from the method by maketrans. Here the characters are replaced with each sensitive spaces.

3: traversing alternative method, as follows

= intab "/ | \> <:?. *" 
title = 'to me / to | a new \ smell of the title?. ' 
For S in intab: 
    IF title in S: 
        Print (S) 
        title title.replace = (S,' ') 
Print (title)

First, traversing the string, and then use the keyword in the title to check whether there are sensitive characters, if present, replace and re-assigned to tiitle, because the replace string methods can not change the contents of the original string. The code output results are as follows:

?
/
|
\
To me as the headline.

4: Summary

windows under the file name can not appear in these sensitive characters *:? "<> \ / |,

If these sensitive character string that needs to exist as a file name, you can use the method to solve the replace method or combination of maketrans and translate

(Ps: still in the early chicken dishes, if the wrong place, feel free to correct me ...... welcome Gangster)

****************************** short step, a thousand miles. ******************************

 

Guess you like

Origin www.cnblogs.com/liangxiyang/p/11355542.html