Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correct way of using os.path.join() in python

Tags:

python

I came across this python function os.path.join(). I wanted to know which was a preferred method of using it.

os.path.join(r'C:\\' , r'some_dir_in_C_folder') 

or

print os.path.join("C:\\" , "some_dir_in_C_folder\\")

TIA

like image 766
Tuffy Avatar asked Jul 30 '14 00:07

Tuffy


People also ask

How does os path join work Python?

path. join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator ('/') following each non-empty part except the last path component.

How do I join a Python operating system?

To join the path in Python, use the os. path. join() function. The os.

What does os path Join mean?

os. path. join combines path names into one complete path. This means that you can merge multiple parts of a path into one, instead of hard-coding every path name manually.


1 Answers

both are incorrect, the correct usage is(for eg: c:/programs/myfiles/cat.txt:

>>> import os
>>> os.path.join('C:/' , 'programs','myfiles','cat.txt') 
'C:/programs/myfiles/cat.txt'
like image 142
suhailvs Avatar answered Oct 08 '22 01:10

suhailvs