Python Forum
[split] Single slash syntax in file path
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Single slash syntax in file path
#1
I am having a similar noob problem.

Setup:
I'm calling the python script with arguments from a different program through the command prompt (this other program does not offer full python integration)
This other program collects a file path, turns it into a string variable and passes it to the python script through an argument
The original path comes with a single slash syntax. The python script wants it as a double slash syntax, but I don't know how to convert it.

any help would be appreciated

Here are my tests

import os
import re

s="C:\Users\alex.ogata\Desktop\test001"

encode=s.encode('unicode-escape')
#r1= re(s)
r2= repr(s)
r3= re.escape(s)
ospath = os.path.normpath(s)
st = str(s)
newStr=s.replace(chr(92),"\\")


print s
print encode
#print r1
print r2
print ospath
print st
print newStr

print "this is what I want" + r"C:\\Users\\alex.ogata\\Desktop\\test001"



this is the python script for reference

# these are the arguments coming int
scriptname, imagePathIn=argv

#this is not working
imagePath = re.escape(imagePathIn)


filenames = [f for f in listdir(imagePath) if isfile(join(imagePath, f))]
print(filenames)
print imagePath

def get_filepaths(directory):

file_paths =

for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath) # Add it to the list.

return file_paths
fileList= get_filepaths (imagePath)


User has been warned for this post. Reason: Hijack thread, no BBcode
Reply
#2
Your code is without indentation.
This is wrong,escape character will give errror.
s="C:\Users\alex.ogata\Desktop\test001"
For file path in Windows always use:
>>> s = r"C:\Users\alex.ogata\Desktop\test001"
>>> s
'C:\\Users\\alex.ogata\\Desktop\\test001'
>>> # or 
>>> s = "C:/Users/alex.ogata/Desktop/test001"
>>> s
'C:/Users/alex.ogata/Desktop/test001'
>>> # or 
>>> s = "C:\\Users\\alex.ogata\\Desktop\\test001"
>>> s
'C:\\Users\\alex.ogata\\Desktop\\test001'
Reply
#3
Thank you, I should have clarified
The string s="C:\Users\alex.ogata\Desktop\test001" is something I am receiving into the python script as a given
it is coming in as a variable called imagePathIn

I'm looking to convert it into 'C:\\Users\\alex.ogata\\Desktop\\test001'

As the previous response showed, you can do: r"C:\Users\alex.ogata\Desktop\test001"
But how do you do that by using the variable imagePathIn and not the original string?



(Aug-09-2017, 02:26 PM)snippsat Wrote: You code is without indentation.
This is wrong,escape character will give errror.
s="C:\Users\alex.ogata\Desktop\test001"
For file path in Windows always use:
>>> s = r"C:\Users\alex.ogata\Desktop\test001"
>>> s
'C:\\Users\\alex.ogata\\Desktop\\test001'
>>> # or 
>>> s = "C:/Users/alex.ogata/Desktop/test001"
>>> s
'C:/Users/alex.ogata/Desktop/test001'
>>> # or 
>>> s = "C:\\Users\\alex.ogata\\Desktop\\test001"
>>> s
'C:\\Users\\alex.ogata\\Desktop\\test001'
Reply
#4
(Aug-09-2017, 02:31 PM)aogata Wrote: it is coming in as a variable called imagePathIn I'm looking to convert it into 'C:\\Users\\alex.ogata\\Desktop\\test001'
You should get(\\) from command line.
Test:
import sys

print 'List: {}'.format(sys.argv)
#  print will only show \
print 'File name {}'.format(sys.argv[1])
# This is what you getting using repl
print 'File name {!r}'.format(sys.argv[1])
Output:
C:\1 λ py -2.7 f.py C:\Users\alex.ogata\Desktop\test001 List: ['f.py', 'C:\\Users\\alex.ogata\\Desktop\\test001'] File name C:\Users\alex.ogata\Desktop\test001 File name 'C:\\Users\\alex.ogata\\Desktop\\test001'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to "tee" (=split) output to screen and into file? pstein 6 1,371 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  File path by adding various variables Mishal0488 2 1,026 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Split pdf in pypdf based upon file regex standenman 1 2,075 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  Script File Failure-Path Error? jerryf 13 3,450 Nov-30-2022, 09:58 AM
Last Post: jerryf
  python Multithreading on single file mg24 3 1,725 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Create multiple/single csv file for each sql records mg24 6 1,387 Sep-29-2022, 08:06 AM
Last Post: buran
  How to split the input taken from user into a single character? mHosseinDS86 3 1,166 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  change backslash into slash in a path paul18fr 7 2,336 Jul-21-2022, 04:26 PM
Last Post: deanhystad
  How to split file by same values from column from imported CSV file? Paqqno 5 2,773 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  [split] Results of this program in an excel file eisamabodian 1 1,574 Feb-11-2022, 03:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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