欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)Window路徑格式轉(zhuǎn)換為L(zhǎng)inux路徑格式的代碼

 更新時(shí)間:2024年07月03日 09:46:43   作者:碼農(nóng)研究僧  
這篇文章主要介紹了Python實(shí)現(xiàn)Window路徑格式轉(zhuǎn)換為L(zhǎng)inux路徑格式的方法,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

前言

將其代碼放到服務(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)文章

最新評(píng)論