2013-07-29 39 views

回答

1

使用str.replace

>>> 'C:/dir\\file.txt'.replace('\\', '/') 
'C:/dir/file.txt' 

應用str.replace到所有的路徑,你會被替換的路徑。

>>> paths = list = ['C:/dir\\file.txt', 'C:/dir\\example.zip', 'C:/dir\\example2.zip'] 
>>> paths = [path.replace('\\', '/') for path in paths] 
>>> paths 
['C:/dir/file.txt', 'C:/dir/example.zip', 'C:/dir/example2.zip'] 
+0

相當肯定str.replace在Python-3貶值(它甚至不是在該手冊3.3) –

+2

@OliverMatthews,你錯了。 http://docs.python.org/3/library/stdtypes#str.replace – falsetru

+0

啊,我明白了。字符串模塊的替換函數已折舊。我的錯。 –

0

你應該使用os.path,而不是用手來操作的路徑,但因爲你必須,在這裏你去:

[re.sub("\\\\",'/',x) for x in list]