Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python path as a string [closed]

Tags:

python

path

I use this module

And I have a function that needs a string. I don't find any function which can give me the total path as a string.

from path import *
import paramiko

if __name__ == "__main__":
    hostname = 'localhost'
    username = '**'
    password = '**'
    port = 22

    transport = paramiko.Transport((hostname, port))
    transport.connect(username = username, password = password)
    sftp = paramiko.SFTPClient.from_transport(transport)

    path = path('~/folder/')
    filename = path.joinpath('foo')
    f = sftp.open(filename, 'r') #open needs a string

Do you have an idea?

like image 224
Slot Avatar asked Feb 03 '14 01:02

Slot


People also ask

What does path () do Python?

The Path() object will convert forward slashes into the correct kind of slash for the current operating system.

How do you represent a path in Python?

On Windows, paths are written using backslashes (\) as the separator between folder names. OS X and Linux, however, use the forward slash (/) as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.

What does ./' mean in path?

the slash / is the directory separator. in every directory there are two directories, namely . ( current directory) and .. ( parent directory) if a path starts with a slash, it means it's the root of the filesystem.

How do you escape space in Python path?

You cannot just put \ in front of any character to escape it. In order to actually print a space, you would have to use '\x20' or you could use '\t' to print a tab.


1 Answers

filename has a method called abspath that returns an object with the absolute path. You can cast that to a string.

...
folder_path = path('~/folder/')
filename = path.joinpath('foo')
f = sftp.open(str(filename), 'r')
like image 115
munk Avatar answered Sep 21 '22 21:09

munk