Home > Software engineering >  path \ and / in windows compared to VS code
path \ and / in windows compared to VS code

Time:07-12

If I browse to a file (such as my python exe path) in file explorer (Windows) and click copy path i get this:

"C:\Users\User_one\Local\envs\lon\python.exe"

When run a file in VScode i see the executable path as this:

c:/Users/User_one/Local/envs/lon/python.exe

Why does VS code show forward slashes / whereas windows file explorer path is backward slashes \ ?

Which is the correct (or preferred) one to use when referencing a file ?

CodePudding user response:

The correct one is not writing/hardcoding the path, but creating it with the os library

import os
file=["C:", "Users", "User_one", "Local", "envs", "lon", "python.exe"]

print(os.path.join(*file))

CodePudding user response:

UNIX uses "/" as the path separator, while web applications are recently used on UNIX systems, so at present, all network addresses use "/" as the separator. Windows uses slash "/" as the parameter mark of DOS command prompt. In order to avoid confusion, it uses "\" as the path separator. So currently, file browsers on Windows systems use "\" as the path separator.

  • Related