GithubHelp home page GithubHelp logo

webpath's Introduction

webpath

Path/os-like interface to SFTP using paramiko.

Example

from webpath import SFTPClient
from getpass import getpass

config = dict(
    hostname="sftp.example.com",
    port=22,
    username="website",
    password=getpass(),
)

with SFTPClient(**config) as sftp:
    path = sftp.Path("/root/")
    print(path.is_dir())  # True, assuming this exists
    
    # Create dir
    ndir = path / "hello"
    print(ndir.is_dir())  # False
    fpath.mkdir()
    print(ndir.is_dir())  # True
    
    # Create file
    fpath = path / "world.txt"
    print(fpath.is_file())  # False
    fpath.touch()
    print(fpath.is_file())  # True
    
    # Write to file
    print(fpath.read_text(encoding="utf-8"))  # ""
    fpath.write_text("Hello world", encoding="utf-8")
    print(fpath.read_text(encoding="utf-8"))  # "Hello world"
    
    # Alternatively
    with fpath.open(mode="wt", encoding="utf-8") as f:
        f.write("Hello world")
    
    # Remove file and directory
    fpath.unlink()
    print(fpath.is_file())  # False
    ndir.rmdir()
    print(ndir.is_dir())  # False
    
    # Clone (recursively) local directory source onto remote directory target
    sftp.put_r("C:/users/user/documents/site/", "/root")
    # To go the other way, use sftp.get_r(remote_root, local_root)
    
    # Remove a remote directory (recursively)
    sftp.rm_r("/root")  # Don't do this in production
    
    # Clone (recursively) local files that are new or have been modified to remote directory, relative to root
    # A file is modified if int(path.stat().st_mtime()) are different.
    sftp.put_diff(local_root, "/root")
    
    # Remove (recursively) remote files that does not exist in the local directory, relative to root
    sftp.rm_diff(local_root, remote_root)

sftp is an SFTPHandler object, which is a subclass of paramiko.sftp_client.SFTPClient. For more properties, read the SFTPClient Documentation.

One notable exception is that chdir has been removed, so SFTPHandler only handles absolute paths.

webpath's People

Contributors

fourpoints avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.