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
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  File path by adding various variables Mishal0488 2 966 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Script File Failure-Path Error? jerryf 13 3,315 Nov-30-2022, 09:58 AM
Last Post: jerryf
  -i option changes sys.path (removes leading empty string '') markanth 6 1,899 Aug-26-2022, 09:27 PM
Last Post: markanth
  Convert string to path using Python 2.7 tester_V 10 6,274 Nov-20-2021, 02:20 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,149 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,620 May-07-2021, 03:26 PM
Last Post: herwin
  Add file to sys.path permanently hcccs 5 8,184 Jan-31-2021, 11:26 AM
Last Post: hcccs
  PyDrive download file path MiniMinnow 0 3,210 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  Add path to a local file in pop-up field pythonscripting 1 1,621 Feb-08-2020, 10:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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