Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String to File Path
#1
I am trying to create a file path from a string using os.path.normpath, however, I need a double \ at the beginning and this function doesn't allow this (from what I understand). Inside the pd.read_excel function I need the following: r'\\srpx\Folder\Data.xls'

Here is the code that is not working:

import os
import pandas as pd

excel_string = str("r'\\srpx\Folder\Data.xls'")
excel_file = os.path.normpath(excel_string)

pd.read_excel(excel_file)
By the way, this works perfectly:

pd.read_excel(r'\\srpx\Folder\Data.xls')
Reply
#2
why do you add str(" and ")?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I probably oversimplified my problem. What I really want is to use a variable to define the folder name. I think to change up the "Folder" piece of the file path everything needs to be a string. I am not sure if there is another way to go about using a variable in a file path.

This is what I really want:

import os
import pandas as pd

folder_name = "Folder"
excel_string = str("r'\\srpx\") + folder_name + str("\Data.xls'")
excel_file = os.path.normpath(excel_string)
 
pd.read_excel(excel_file)
Reply
#4
you keep doing the same
the idea of r in front of string is that it's a raw string. But you enclose it in double quotes and make it useless as path
also ne need to use str() because everything you have is already a str.
Finally, the best is to use os.path.join()


>>> folder_name = 'some_folder'
>>> os.path.join(r'\\sprx', folder_name, 'Data.xls')
'\\\\sprx/some_folder/Data.xls'
and before you ask - \\\\ are actually 2 escaped backslahes
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
This worked great. Thanks so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 130 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,673 May-07-2021, 03:26 PM
Last Post: herwin
  Add file to sys.path permanently hcccs 5 1,334 Jan-31-2021, 11:26 AM
Last Post: hcccs
  PyDrive download file path MiniMinnow 0 1,616 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  Add path to a local file in pop-up field pythonscripting 1 735 Feb-08-2020, 10:57 PM
Last Post: Larz60+
  How to get file name without the full path details and without extension aruncom2006 1 3,094 Jan-13-2020, 07:37 AM
Last Post: Larz60+
  How to include Variable in File Path penahuse 3 3,312 Jan-05-2020, 03:08 AM
Last Post: ichabod801
  How to search full path of specified file SriRajesh 3 1,089 Dec-14-2019, 04:52 PM
Last Post: Gribouillis
  how do i write a cross-platform relative file path? pseudo 2 1,208 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  File Path not recognised starzar 3 1,632 Jul-27-2019, 05:23 AM
Last Post: starzar

Forum Jump:

User Panel Messages

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