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

Python實現(xiàn)Window路徑格式轉換為Linux路徑格式的代碼

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

前言

將其代碼放到服務器上,批量的路徑格式需要進行轉換,對應就需要一個腳本

實戰(zhàn)中演示W(wǎng)indow格式轉換為Linux(批量進行局部轉換)

1. 局部轉換

適用單個文件的路徑格式

# 示例路徑
linux_path = "/home/user/documents/project"

# 將Linux路徑中的斜杠轉換為Windows路徑格式
windows_path = linux_path.replace("/", "\\")

# 打印結果
print(windows_path)  # 輸出: \home\user\documents\project

截圖如下:

整體的路徑轉換成想要的

# 示例路徑
linux_path = "/home/user/documents/project"

# 將Linux路徑轉換為Windows路徑
windows_path = linux_path.replace("/home/user/documents", "C:\\Users\\user\\Documents")

# 打印結果
print(windows_path)  # 輸出: C:\Users\user\Documents\project

截圖如下:

2. 批量轉換

def replace_paths_in_file(input_file, output_file, old_path, new_path):
    # 讀取輸入文件內容
    with open(input_file, 'r') as file:
        file_content = file.read()

    # 轉換路徑
    updated_content = file_content.replace(old_path, new_path)

    # 寫入到輸出文件
    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"

# 調用函數(shù)進行轉換
replace_paths_in_file(input_file, output_file, old_linux_path, new_windows_path)

如果是未知的路徑,則對應需要使用正則表達式進行處理

import re

def replace_paths_in_file(input_file, output_file):
    # 定義正則表達式來匹配Linux路徑
    linux_path_pattern = re.compile(r'(/[^/ ]*)+')
    
    def linux_to_windows_path(linux_path):
        # 將Linux路徑中的斜杠轉換為Windows路徑格式
        windows_path = linux_path.replace("/", "\\")
        # 例如,如果需要將某個特定的根路徑轉換為Windows的根路徑
        windows_path = windows_path.replace("\\home\\user\\documents", "C:\\Users\\user\\Documents")
        return windows_path
    
    # 讀取輸入文件內容
    with open(input_file, 'r') as file:
        file_content = file.read()
    
    # 使用正則表達式轉換所有匹配的路徑
    updated_content = linux_path_pattern.sub(lambda match: linux_to_windows_path(match.group(0)), file_content)
    
    # 寫入到輸出文件
    with open(output_file, 'w') as file:
        file.write(updated_content)

# 示例文件路徑
input_file = 'input.txt'
output_file = 'output.txt'

# 調用函數(shù)進行轉換
replace_paths_in_file(input_file, output_file)

3. 實戰(zhàn)Demo

原本在Window上跑的Yolov5,對應的路徑不是Linux,如果要放到Linux上跑,需要轉換為Linux的路徑
但是他的標簽路徑起碼幾千條(VOC的數(shù)據(jù)集標簽)

對應的代碼如下:(只需批量的轉換局部即可)

def convert_path(windows_path):
    # 轉換基路徑
    linux_path = windows_path.replace('F:\\AI\\datasets\\VOC2007', '/home/l228/huoyanhao/yolov5/datasets/VOC/images')
    
    # 將路徑分隔符從反斜杠和混合斜杠轉換為統(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)

將其轉換成吱聲想要的路徑,截圖如下:

到此這篇關于Python實現(xiàn)Window路徑格式轉換為Linux路徑格式的代碼的文章就介紹到這了,更多相關Python Window格式轉Linux內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論