I have a script in which I request the directory, name and extension of a file from the keyboard and then mount it and assign it to a variable:
import numpy as np
import f06_directories as drc
# Pedimos al usuario que introduzca el directorio en el que se encuentra el
# archivo que contiene los datos.
direct = drc.assembling_direct_arch()
The functions I use are the following:
def assembling_direct_arch(name=None, extension=None, direct=None):
"""
Genera una variable en la que se almacena el directorio de un archivo
con su nombre y extension introducidos por teclado por el usuario.
Parameters
----------
name: str
Nombre del archivo.
extension: str
Extension del archivo.
direct: str
Directorio del archivo.
Returns
----------
assemble: str
Directorio con nombre y extension del archivo.
"""
#
if name is None:
name = name_arch()
if extension is None:
extension = extension_arch(name)
if direct is None:
direct = direct_arch()
#
assemble = direct + '\\' + name + extension
#
return assemble
def direct_arch():
"""
Genera una variable en la que se almacena el directorio, introducido
por teclado por el usuario.
"""
#
# Pedimos al usuario que introduzca el directorio.
direct = input('Introduzca el directorio:\n')
#
return direct
def extension_arch(name):
"""
Genera una variable en la que se almacena la extension de un archivo
introducido por teclado por el usuario.
Parameters
----------
name: str
Nombre del archivo.
"""
#
# Pedimos al usuario que introduzca la extension del archivo.
extension = input('Introduzca la extension del archivo "' + name
+ '" (incluyendo el punto):\n')
#
return extension
def name_arch():
"""
Genera una variable en la que se almacena el nombre de un archivo
introducido por teclado por el usuario.
"""
#
# Pedimos al usuario que introduzca el nombre del archivo.
name = input('Introduzca el nombre del archivo:\n')
#
return name
The problem is that if I pass the following directory, name and extension:
Z:\Jobs\Python 3\functions\f00_general_functions
a
.CD
The direct variable has the value:
Z:\\Trabajos\\Python 3\\funciones\\f00_funciones_generales\\a.cd
That is, where there should be '\'
, we have '\\'
.
How do I fix this problem?
Actually there is no problem.
If we print the variable by doing
print(direct)
we get the value of the string stored indirect
which is exactly what we wanted to get:If, on the other hand, we execute the variable directly in the console,
direct
what it returns is the "representable" version of said variable, which will also appear in quotes. This is so because the representable version must be valid python code (as you would write it in your source code, where the \ must be inserted twice to "escape" that special character):Another way of explaining it is how @abulafia does it in the comments and which I partially copy below: