1

I would like to write this code on windows : os.path.join(folder1 + "/" + folder2)

it works fine in MAC but in windows it gives me an error: OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\Users\Khalaf\Desktop\test\dataset-images\x.jpg' -> 'C:\Users\Khalaf\Desktop\test\dataset-images\C:\Users\Khalaf\Desktop\test\dataset-images-1.jpg'

Khalaf90
  • 25
  • 2
  • 9

4 Answers4

2
os.path.join(folder1 + "\\" + folder2)

MAC and Linux work with singe /

but in Windows we have to pass \\

Try these it will work

Harsh Parekh
  • 103
  • 6
  • i did, it gives me : OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\Khalaf\\Desktop\\test\\dataset-images\\x.jpg' -> 'C:\\Users\\Khalaf\\Desktop\\test\\dataset-images\\C:\\Users\\Khalaf\\Desktop\\test\\dataset-images-1.jpg' – Khalaf90 May 31 '21 at 11:19
  • Please share folder1 and folder2 path – Harsh Parekh May 31 '21 at 11:27
  • 1
    i solved it using : os.path.basename, thank you very much for your tips – Khalaf90 May 31 '21 at 11:43
0

seperate the two folders with commas

os.path.join(folder1, folder2)

The function creates the equivalent for windows which i think is \

0

I am trying to learn python and am making a program that will output a script. I want to use os.path.join, but am pretty confused.

os.path.join('c:', 'sourcedir')

when I use the copytree command, Python will output it the desired way, for example:

import shutil

src = os.path.join('c:', 'src')

dst = os.path.join('c:', 'dst')

shutil.copytree(src, dst)

Windows has a concept of current directory for each drive. Because of that, "c:sourcedir" means "sourcedir" inside the current C: directory, and you'll need to specify an absolute directory.

Any of these should work and give the same result, but I don't have a Windows VM fired up at the moment to double check:

"c:/sourcedir"

os.path.join("/", "c:", "sourcedir")

os.path.join("c:/", "sourcedir")

0

Tried below on windows:

user= os.getlogin()
os.path.join("C:\\Users",user,"TestAutomationDownloads")

Result - A directory got created in windows at path c:Users\USERNAME\TestAutomationDownloads where username gets its value from variable user =os.getlogin() os.path.join adds "\" itself before and after variable.

Javad
  • 2,033
  • 3
  • 13
  • 23