I do agree with @Gribouillis
Can show both way,as you should have used
If look closer at it,so is the first one a string,
and second on a Path object that show it'a a Windows Path.
The right way is to use
Can show both way,as you should have used
os.path.join()
in your first code.from os import path file_path = 'C:\\foo' #input("Enter the file path: ") file_name = 'test.txt' #input("Enter the file name with extension (.txt): ") full_path = path.join(file_path, file_name) print(f"Full file address is: {full_path}") #--------------------------------------------- from pathlib import Path file_path = Path('C:\\foo') #Path(input("Enter the file path: ").strip()) file_name = 'test.txt' #input('Enter the file name with extension (.txt): ').strip() file_full_path = file_path / file_name print("Full file address is", file_full_path)
Output:Full file address is: C:\foo\test.txt
Full file address is C:\foo\test.txt
So the output is the "same" almost.If look closer at it,so is the first one a string,
and second on a Path object that show it'a a Windows Path.
>>> full_path 'C:\\foo\\test.txt' >>> file_full_path WindowsPath('C:/foo/test.txt')What if need that Path object should be a string?
The right way is to use
os.fspath(path)
and not str
.>>>import os >>> os.fspath(file_full_path) 'C:\\foo\\test.txt' >>> # Can also to it without import >>> file_full_path.__fspath__() 'C:\\foo\\test.txt'