Python實(shí)現(xiàn)Window路徑格式轉(zhuǎn)換為L(zhǎng)inux路徑格式的代碼
前言
將其代碼放到服務(wù)器上,批量的路徑格式需要進(jìn)行轉(zhuǎn)換,對(duì)應(yīng)就需要一個(gè)腳本
實(shí)戰(zhàn)中演示W(wǎng)indow格式轉(zhuǎn)換為L(zhǎng)inux(批量進(jìn)行局部轉(zhuǎn)換)
1. 局部轉(zhuǎn)換
適用單個(gè)文件的路徑格式
# 示例路徑
linux_path = "/home/user/documents/project"
# 將Linux路徑中的斜杠轉(zhuǎn)換為Windows路徑格式
windows_path = linux_path.replace("/", "\\")
# 打印結(jié)果
print(windows_path) # 輸出: \home\user\documents\project
截圖如下:

整體的路徑轉(zhuǎn)換成想要的
# 示例路徑
linux_path = "/home/user/documents/project"
# 將Linux路徑轉(zhuǎn)換為Windows路徑
windows_path = linux_path.replace("/home/user/documents", "C:\\Users\\user\\Documents")
# 打印結(jié)果
print(windows_path) # 輸出: C:\Users\user\Documents\project
截圖如下:

2. 批量轉(zhuǎn)換
def replace_paths_in_file(input_file, output_file, old_path, new_path):
# 讀取輸入文件內(nèi)容
with open(input_file, 'r') as file:
file_content = file.read()
# 轉(zhuǎn)換路徑
updated_content = file_content.replace(old_path, new_path)
# 寫(xiě)入到輸出文件
with open(output_file, 'w') as file:
file.write(updated_content)
# 示例文件路徑
input_file = 'input.txt'
output_file = 'output.txt'
# 示例路徑
old_linux_path = "/home/user/documents"
new_windows_path = "C:\\Users\\user\\Documents"
# 調(diào)用函數(shù)進(jìn)行轉(zhuǎn)換
replace_paths_in_file(input_file, output_file, old_linux_path, new_windows_path)
如果是未知的路徑,則對(duì)應(yīng)需要使用正則表達(dá)式進(jìn)行處理
import re
def replace_paths_in_file(input_file, output_file):
# 定義正則表達(dá)式來(lái)匹配Linux路徑
linux_path_pattern = re.compile(r'(/[^/ ]*)+')
def linux_to_windows_path(linux_path):
# 將Linux路徑中的斜杠轉(zhuǎn)換為Windows路徑格式
windows_path = linux_path.replace("/", "\\")
# 例如,如果需要將某個(gè)特定的根路徑轉(zhuǎn)換為Windows的根路徑
windows_path = windows_path.replace("\\home\\user\\documents", "C:\\Users\\user\\Documents")
return windows_path
# 讀取輸入文件內(nèi)容
with open(input_file, 'r') as file:
file_content = file.read()
# 使用正則表達(dá)式轉(zhuǎn)換所有匹配的路徑
updated_content = linux_path_pattern.sub(lambda match: linux_to_windows_path(match.group(0)), file_content)
# 寫(xiě)入到輸出文件
with open(output_file, 'w') as file:
file.write(updated_content)
# 示例文件路徑
input_file = 'input.txt'
output_file = 'output.txt'
# 調(diào)用函數(shù)進(jìn)行轉(zhuǎn)換
replace_paths_in_file(input_file, output_file)
3. 實(shí)戰(zhàn)Demo
原本在Window上跑的Yolov5,對(duì)應(yīng)的路徑不是Linux,如果要放到Linux上跑,需要轉(zhuǎn)換為L(zhǎng)inux的路徑
但是他的標(biāo)簽路徑起碼幾千條(VOC的數(shù)據(jù)集標(biāo)簽)

對(duì)應(yīng)的代碼如下:(只需批量的轉(zhuǎn)換局部即可)
def convert_path(windows_path):
# 轉(zhuǎn)換基路徑
linux_path = windows_path.replace('F:\\AI\\datasets\\VOC2007', '/home/l228/huoyanhao/yolov5/datasets/VOC/images')
# 將路徑分隔符從反斜杠和混合斜杠轉(zhuǎn)換為統(tǒng)一的斜杠
linux_path = linux_path.replace('\\', '/')
return linux_path
def batch_convert_paths(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line in infile:
windows_path = line.strip()
linux_path = convert_path(windows_path)
outfile.write(linux_path + '\n')
# 輸入文件和輸出文件路徑
input_file = 'windows_paths.txt'
output_file = 'linux_paths.txt'
batch_convert_paths(input_file, output_file)
將其轉(zhuǎn)換成吱聲想要的路徑,截圖如下:

到此這篇關(guān)于Python實(shí)現(xiàn)Window路徑格式轉(zhuǎn)換為L(zhǎng)inux路徑格式的代碼的文章就介紹到這了,更多相關(guān)Python Window格式轉(zhuǎn)Linux內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm打開(kāi)長(zhǎng)代碼文件CPU占用率過(guò)高的解決
這篇文章主要介紹了pycharm打開(kāi)長(zhǎng)代碼文件CPU占用率過(guò)高的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python中創(chuàng)建相關(guān)系數(shù)矩陣的方法小結(jié)
相關(guān)系數(shù)矩陣是一種用于衡量變量之間關(guān)系的重要工具,本文將介紹在 Python 中創(chuàng)建相關(guān)系數(shù)矩陣的不同方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
Win10下安裝CUDA11.0+CUDNN8.0+tensorflow-gpu2.4.1+pytorch1.7.0+p
這篇文章主要介紹了Win10下安裝CUDA11.0+CUDNN8.0+tensorflow-gpu2.4.1+pytorch1.7.0+paddlepaddle-gpu2.0.0,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
python實(shí)現(xiàn)畫(huà)一顆樹(shù)和一片森林
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)畫(huà)一顆樹(shù)和一片森林,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式
這篇文章主要介紹了pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Django Auth應(yīng)用實(shí)現(xiàn)用戶身份認(rèn)證
Django Auth 應(yīng)用一般用在用戶的登錄注冊(cè)上,用于判斷當(dāng)前的用戶是否合法。本文將介紹Auth的另一個(gè)功能,即認(rèn)證用戶身份,感興趣的同學(xué)可以關(guān)注一下2021-12-12
Python中創(chuàng)建包和增添包的路徑(sys.path.append())
本文主要介紹了Python中創(chuàng)建包和增添包的路徑(sys.path.append()),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
python實(shí)現(xiàn)用于測(cè)試網(wǎng)站訪問(wèn)速率的方法
這篇文章主要介紹了python實(shí)現(xiàn)用于測(cè)試網(wǎng)站訪問(wèn)速率的方法,涉及Python中urllib2模塊及時(shí)間的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05

