Convert Windows path to Linux


Ernil Uzbek:

I created a CSV file ( driving_log.csvwith a file path for each image created by my emulator), but I was using my brother's Windows computer when I did this, so now the file path looks like this for each image (And almost 14,000 people). When I call it from my file.pycsv file, I get an error, which is in the same directory.

C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\center_2020_02_08_14_16_38_988.jpg,C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\left_2020_02_08_14_16_38_988.jpg,C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\right_2020_02_08_14_16_38_988.jpg,0,0,0,7.918889E-06

I use Arch Linux. I want to iterate through the lines and change it to this:

/home/onur/Documents/behavioral-cloning-CARLA/IMG/center_2020_02_08_14_16_38_988.jpg,left_2020_02_08_14_16_38_988.jpg,right_2020_02_08_14_16_38_988.jpg,0,0,0,7.918889E-06

As you can see, I need everything that comes after \IMG\.

What's the best way to go about this? What if I just slice each string IMG\and take a post or use a regex?

I've tried this for this particular line to see if it works, but it doesn't:

string = r"C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\center_2020_02_08_14_16_38_988.jpg,C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\left_2020_02_08_14_16_38_988.jpg,C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\right_2020_02_08_14_16_38_988.jpg,0,0,0,7.918889E-06 "

new_string = string.replace(r"'C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\'", r"'/home/onur/Documents/behavioral-cloning-CARLA/IMG/'", 3)

print(new_string)

Here is the output:

C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\center_2020_02_08_14_16_38_988.jpg,C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\left_2020_02_08_14_16_38_988.jpg,C:\Users\a-ozb\Desktop\onur\behavirol-cloning-carla\IMG\right_2020_02_08_14_16_38_988.jpg,0,0,0,7.918889E-06 
Selcuk:

If you are running the code on a *nix machine, you can use the class:PureWindowsPath

>>> from pathlib import PureWindowsPath, PurePosixPath
>>> path = PureWindowsPath('C:\\Users\\foo\\bar')

>>> path.parts
('c:\\', 'Users', 'foo', 'bar')

>>> PurePosixPath('/usr', *path.parts[2:])
PurePosixPath('/usr/foo/bar')

You can put the string replace method in each line of the text file like this:

with open("input.csv", "r") as f_in:
    with open("output.csv", "w") as f_out:
        for line in f_in:
            new_line = line.replace(...)  # magic goes here
            f_out.write("{}\n".format(new_line))

Related


Convert Windows path to Linux

Ernil Uzbek: I created a CSV file ( driving_log.csvwith a file path for each image created by my emulator), but I was using my brother's Windows computer when I did this, so now the file path looks like this for each image (And almost 14,000 people). When I ca

Convert Windows path to Linux

Ernil Uzbek: I created a CSV file ( driving_log.csvwith a file path for each image created by my emulator), but I was using my brother's Windows computer when I did this, so now the file path looks like this for each image (And almost 14,000 people). When I ca

Convert Windows path to Linux

Ernil Uzbek: I created a CSV file ( driving_log.csvwith a file path for each image created by my emulator), but I was using my brother's Windows computer when I did this, so now the file path looks like this for each image (And almost 14,000 people). When I ca

Convert Windows path to Linux

Ernil Uzbek: I created a CSV file ( driving_log.csvwith a file path for each image created by my emulator), but I was using my brother's Windows computer when I did this, so now the file path looks like this for each image (And almost 14,000 people). When I ca

Convert Windows path to Linux

Ernil Uzbek: I created a CSV file ( driving_log.csvwith a file path for each image created by my emulator), but I was using my brother's Windows computer when I did this, so now the file path looks like this for each image (And almost 14,000 people). When I ca

Bash script to convert Windows path to Linux path

username I'm new to shell scripting and trying to do the following, converting a windows path to a linux path and navigating to that location: Input: cdwin "J:\abc\def"Action:cd /usr/abc/def/ So I will change the following: "J:" -> "/usr" and "\" -> "/" Here

Convert Windows created ZIP to Linux (internal path issue)

Slavic I created a .zip on a Windows computer (outside of the control). The zip file contains paths that need to be preserved when unzipping. However, when I unzip, all files end up like this: unzip_dir/\window\path\separator\myfile.ext I tried it with or with

Convert Windows created ZIP to Linux (internal path issue)

Slavic I created a .zip on a Windows computer (outside of the control). The zip file contains paths that need to be preserved when unzipping. However, when I unzip, all files end up like this: unzip_dir/\window\path\separator\myfile.ext I tried it with or with

CMake convert Unix to Windows path

read I'm trying to convert a unix-style MSYS path to a /c/my/path/to/a/folderWindows path, or something CMake would understand to a Windows path C:/my/path/to/a/folder. I want it to work on the already correct path. Is there any proper way? Note: Please do not

CMake convert Unix to Windows path

read I'm trying to convert a Unix-style MSYS path (for example) /c/my/path/to/a/folderto a Windows path, or something CMake would understand to a Windows path C:/my/path/to/a/folder. I want it to work on the already correct path. Is there any proper way? Note:

CMake convert Unix to Windows path

read I'm trying to convert a unix-style MSYS path to a /c/my/path/to/a/folderWindows path, or something CMake would understand to a Windows path C:/my/path/to/a/folder. I want it to work on the already correct path. Is there any proper way? Note: Please do not

Convert filesystem::path to char* on Windows

Johannes The filesystem in C++17 is based on Boost.Filesystem. I'm using it now on Windows with VS2017. #include <filesystem> namespace fs = std::experimental::filesystem; I traverse the directory for (auto& p: fs::directory_iterator("media")) I want to pass

CMake convert Unix to Windows path

read I'm trying to convert a Unix-style MSYS path (for example) /c/my/path/to/a/folderto a Windows path, or something CMake would understand to a Windows path C:/my/path/to/a/folder. I want it to work on the already correct path. Is there any proper way? Note:

CMake convert Unix to Windows path

read I'm trying to convert a unix-style MSYS path to a /c/my/path/to/a/folderWindows path, or something CMake would understand to a Windows path C:/my/path/to/a/folder. I want it to work on the already correct path. Is there any proper way? Note: Please do not

Convert filesystem::path to char* on Windows

Johannes The filesystem in C++17 is based on Boost.Filesystem. I'm using it now on Windows with VS2017. #include <filesystem> namespace fs = std::experimental::filesystem; I traverse the directory for (auto& p: fs::directory_iterator("media")) I want to pass

Map Windows path to VirtualBox's Linux path

model pad I am trying to sync my VirtualBox computer between Linux + Windows. However, the following configuration files are displayed in Windows directory format. My Linux location is /media/large/VirtualBoxWindows D:\VirtualBox. Is there any way to map D:\to

Map Windows path to VirtualBox's Linux path

model pad I am trying to sync my VirtualBox computer between Linux + Windows. However, the following configuration files are displayed in Windows directory format. My Linux location is /media/large/VirtualBoxWindows D:\VirtualBox. Is there any way to map D:\to

Convert Windows path for Windows Ubuntu Bash

delayed reflex I have a Windows batch script using Windows Ubuntu Bash. It receives the full Windows path as an argument and then passes that path to the command in Ubuntu Bash. @echo off bash -lic 'ffmpeg -i "%1" output.avi' This "%1"is the full Windows path

Convert Windows path for Windows Ubuntu Bash

delayed reflex I have a Windows batch script using Windows Ubuntu Bash. It receives the full Windows path as an argument and then passes that path to the command in Ubuntu Bash. @echo off bash -lic 'ffmpeg -i "%1" output.avi' This "%1"is the full Windows path

Get the basename of a Windows path in Linux

Ahmad Siavashi Suppose I have a string that holds the address of a Windows file, let's say local_address = "C:\\TEMP\\filename.txt" Retrieving the filename from the above address I used import os filename = os.path.basename(local_address) In Windows, whe

Get the basename of a Windows path in Linux

Ahmad Siavashi Suppose I have a string that holds the address of a Windows file, let's say local_address = "C:\\TEMP\\filename.txt" Retrieving the filename from the above address I used import os filename = os.path.basename(local_address) In Windows, whe

Get the basename of a Windows path in Linux

Ahmad Siavashi Suppose I have a string that holds the address of a Windows file, let's say local_address = "C:\\TEMP\\filename.txt" Retrieving the filename from the above address I used import os filename = os.path.basename(local_address) In Windows, whe

Get the basename of a Windows path in Linux

Ahmad Siavashi Suppose I have a string that holds the address of a Windows file, let's say local_address = "C:\\TEMP\\filename.txt" Retrieving the filename from the above address I used import os filename = os.path.basename(local_address) In Windows, whe

Windows Subsystem Linux VSCode Path

Spencer 10 Trying to install Visual Studio Code on Windows Subsystem for Linux is a mess for me. Before diving in, I can code .open VSCode in the current directory using the terminal command, which can be used in cmd, bash, zsh or fish Since trying to install

Java path in Windows for Linux environment

AsfK I'm developing in Windows and have to access an sftp (Linux) server somewhere in the process. I did some logic to prepare the filenames that have to be copied from the sftp server and need to generate the full path, so I wrote this line in the code: Paths

Get the basename of a Windows path in Linux

Ahmad Siavashi Suppose I have a string that holds the address of a Windows file, let's say local_address = "C:\\TEMP\\filename.txt" Retrieving the filename from the above address I used import os filename = os.path.basename(local_address) In Windows, whe

Get the basename of a Windows path in Linux

Ahmad Siavashi Suppose I have a string that holds the address of a Windows file, let's say local_address = "C:\\TEMP\\filename.txt" Retrieving the filename from the above address I used import os filename = os.path.basename(local_address) In Windows, whe

Windows Subsystem Linux VSCode Path

Spencer 10 Trying to install Visual Studio Code on Windows Subsystem for Linux is a mess for me. Before diving in, I can code .open VSCode in the current directory using the terminal command, which can be used in cmd, bash, zsh or fish Since trying to install

How to convert windows path to posix path using node path

dishonest I'm developing on Windows, but need to know how to convert Windows paths (with backslashes ) to POSIX paths \with forward slashes ( /) ? My goal is to convert C:\repos\vue-t\tests\views\index\home.vuetoC:/repos/vue-t/tests/views/index/home.vue so I c