I am trying to name a text file after the last directory in a file path. I need to be able to get this name for OS's that use either forward or backward slashes. I've successfully used the string split() method for forward slash file paths but not backslash. Also, when I try to print the backslash directory name the slashes are not included (probably reading as escape sequences); however, when I use re.search() method it still finds the slash.
How do I fix this?
See code below:
import re
import sys
targetDirectory = sys.argv[1]
filePathDirectorySeparator = ''
usesForwardSlashSeparator = re.search('/', targetDirectory)
if(usesForwardSlashSeparator):
filePathDirectorySeparator = '/'
else:
filePathDirectorySeparator = '\\'
fileName = targetDirectory.split(filePathDirectorySeparator)[-1]
file = open(fileName, 'w+')
file.write('HELLO')
file.close()
The link shows my console output (I'm not allowed to embed images yet apparently...)