Python Forum
File path by adding various variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File path by adding various variables
#1
Hi Guys

This may be a really simple issue which has stumped me.
The "FPath" variable has two back slashes at the end.
In the current setup the code will run, however with the double backslash the file path is invalid.
Using one backslash at the end does not work as errors are developed

The "FName" variable will be defined from a spreadsheet, however below I have just assigned it a name for the sake of this post.

How can I set up the code so that the file path will be valid?


FPath = r"C:\Users\mishal.mohanlal\Desktop\\"
FName = "Structure1.STD"
F = FPath+FName
print(F)
Reply
#2
Should not use double backslash(last) when use raw string,should be nothing.
If use pathlib then it dos not matter it will fix it.
>>> import pathlib
>>> 
>>> FPath = r"C:\Users\mishal.mohanlal\Desktop\\"
>>> FPath
'C:\\Users\\mishal.mohanlal\\Desktop\\\\'
>>> FName = "Structure1.STD"
>>> file_path = pathlib.PurePath(FPath, FName)
>>> file_path
PureWindowsPath('C:/Users/mishal.mohanlal/Desktop/Structure1.STD')
>>> print(file_path)
C:\Users\mishal.mohanlal\Desktop\Structure1.STD
So this is a new and better of doing what os did before with os.path.join()
Example
>>> import os
>>> 
>>> FPath = r"C:\Users\mishal.mohanlal\Desktop"
>>> FName = "Structure1.STD"
>>> os.path.join(FPath, FName)
'C:\\Users\\mishal.mohanlal\\Desktop\\Structure1.STD'
So this way will not fix the double backslash usage as pathlib dos,so here i use without.
Of course both work in pathlib.
>>> import pathlib
>>> 
>>> FPath = r"C:\Users\mishal.mohanlal\Desktop"
>>> FName = "Structure1.STD"
>>> file_path = pathlib.PurePath(FPath, FName)
>>> file_path
PureWindowsPath('C:/Users/mishal.mohanlal/Desktop/Structure1.STD')
>>> print(file_path)
C:\Users\mishal.mohanlal\Desktop\Structure1.STD
Reply
#3
You are not allowed to end a string literal with an odd number of backslashes. It doesn't matter if the string is raw or not. So if you need a single backslash at the end of a string, you cannot use a raw string.
FPath = "C:\\Users\\mishal.mohanlal\\Desktop\\"   # this works
Among the other options mentioned (I like pathlib the best), you can also use forward slashes. This will work as a file path in windows even though windows uses backslashes in file paths. Python fixes the separator when opening a file. It addition to avoiding the escape sequence nightmare, this lets you write code that runs on windows an linux.
FPath = "C:/Users/mishal.mohanlal/Desktop/"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script File Failure-Path Error? jerryf 13 3,475 Nov-30-2022, 09:58 AM
Last Post: jerryf
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,745 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  Adding to an XML file TeXaSpEtE83 0 1,276 Dec-22-2021, 08:28 AM
Last Post: TeXaSpEtE83
Bug how can i adding custom path for saving the zip_file demonvictor 1 1,484 Oct-09-2021, 09:37 AM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,222 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 15,123 May-07-2021, 03:26 PM
Last Post: herwin
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,953 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Add file to sys.path permanently hcccs 5 8,439 Jan-31-2021, 11:26 AM
Last Post: hcccs
  How to rename a CSV file by adding MODIFIED in the filename? Python_User 25 8,170 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  adding properties to variables rudihammad 0 1,693 May-06-2020, 05:09 AM
Last Post: rudihammad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020