Python Paramiko SFTP get file along with file timestamp/stat











up vote
1
down vote

favorite
1












# create SSHClient instance
ssh = paramiko.SSHClient()

list =

# AutoAddPolicy automatically adding the hostname and new host key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(hostname, port, username, password)
stdin, stdout, stderr = ssh.exec_command("cd *path*; ls")

for i in stdout:
list.append(i)

sftp = ssh.open_sftp()

for i in list:
tempremote = ("*path*" + i).replace('n', '')
templocal = ("*path*" + i).replace('n', '')

try:
#Get the file from the remote server to local directory
sftp.get(tempremote, templocal)
except Exception as e:
print(e)



Remote Server File Date Modified Stat : 6/10/2018 10:00:17



Local File Date Modified Stat : Current datetime




But I found that the date modified changed after done copy the file.



Is there anyway to copy remote file along with the file stat to the local file too ?










share|improve this question




















  • 1




    Why are you using shell ls command to retrieve file a list? Use SFTP: SFTPClient.listdir.
    – Martin Prikryl
    Nov 8 at 7:49















up vote
1
down vote

favorite
1












# create SSHClient instance
ssh = paramiko.SSHClient()

list =

# AutoAddPolicy automatically adding the hostname and new host key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(hostname, port, username, password)
stdin, stdout, stderr = ssh.exec_command("cd *path*; ls")

for i in stdout:
list.append(i)

sftp = ssh.open_sftp()

for i in list:
tempremote = ("*path*" + i).replace('n', '')
templocal = ("*path*" + i).replace('n', '')

try:
#Get the file from the remote server to local directory
sftp.get(tempremote, templocal)
except Exception as e:
print(e)



Remote Server File Date Modified Stat : 6/10/2018 10:00:17



Local File Date Modified Stat : Current datetime




But I found that the date modified changed after done copy the file.



Is there anyway to copy remote file along with the file stat to the local file too ?










share|improve this question




















  • 1




    Why are you using shell ls command to retrieve file a list? Use SFTP: SFTPClient.listdir.
    – Martin Prikryl
    Nov 8 at 7:49













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





# create SSHClient instance
ssh = paramiko.SSHClient()

list =

# AutoAddPolicy automatically adding the hostname and new host key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(hostname, port, username, password)
stdin, stdout, stderr = ssh.exec_command("cd *path*; ls")

for i in stdout:
list.append(i)

sftp = ssh.open_sftp()

for i in list:
tempremote = ("*path*" + i).replace('n', '')
templocal = ("*path*" + i).replace('n', '')

try:
#Get the file from the remote server to local directory
sftp.get(tempremote, templocal)
except Exception as e:
print(e)



Remote Server File Date Modified Stat : 6/10/2018 10:00:17



Local File Date Modified Stat : Current datetime




But I found that the date modified changed after done copy the file.



Is there anyway to copy remote file along with the file stat to the local file too ?










share|improve this question















# create SSHClient instance
ssh = paramiko.SSHClient()

list =

# AutoAddPolicy automatically adding the hostname and new host key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(hostname, port, username, password)
stdin, stdout, stderr = ssh.exec_command("cd *path*; ls")

for i in stdout:
list.append(i)

sftp = ssh.open_sftp()

for i in list:
tempremote = ("*path*" + i).replace('n', '')
templocal = ("*path*" + i).replace('n', '')

try:
#Get the file from the remote server to local directory
sftp.get(tempremote, templocal)
except Exception as e:
print(e)



Remote Server File Date Modified Stat : 6/10/2018 10:00:17



Local File Date Modified Stat : Current datetime




But I found that the date modified changed after done copy the file.



Is there anyway to copy remote file along with the file stat to the local file too ?







python sftp paramiko getfiles






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 7:42









Martin Prikryl

83.3k22154342




83.3k22154342










asked Nov 8 at 2:42









Jack Lim

478




478








  • 1




    Why are you using shell ls command to retrieve file a list? Use SFTP: SFTPClient.listdir.
    – Martin Prikryl
    Nov 8 at 7:49














  • 1




    Why are you using shell ls command to retrieve file a list? Use SFTP: SFTPClient.listdir.
    – Martin Prikryl
    Nov 8 at 7:49








1




1




Why are you using shell ls command to retrieve file a list? Use SFTP: SFTPClient.listdir.
– Martin Prikryl
Nov 8 at 7:49




Why are you using shell ls command to retrieve file a list? Use SFTP: SFTPClient.listdir.
– Martin Prikryl
Nov 8 at 7:49












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Paramiko indeed won't preserve timestamp when transferring files.



You have to explicitly call the os.utime after the download.





Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.get() method.



You can reuse their implementation (code simplified by me):



sftpattrs = sftp.stat(tempremote)
os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))




Similarly for uploads.






share|improve this answer



















  • 1




    Thanks this is what I need in my function. Because I need the date modified to process my files.
    – Jack Lim
    Nov 8 at 8:24


















up vote
1
down vote













There doesn't seem to be a way to copy with the stats documented in the paramiko SFTP module. It makes sense why though, because copying the stats besides times for a remote file wouldn't necessarily make sense (i.e. the user/group ids would not make sense on your local machine).



You can just copy the file, then get the atime/mtime/ctime using the SFTP client's stat or lstat methods and set those on the local file using os.utime.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200800%2fpython-paramiko-sftp-get-file-along-with-file-timestamp-stat%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Paramiko indeed won't preserve timestamp when transferring files.



    You have to explicitly call the os.utime after the download.





    Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.get() method.



    You can reuse their implementation (code simplified by me):



    sftpattrs = sftp.stat(tempremote)
    os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))




    Similarly for uploads.






    share|improve this answer



















    • 1




      Thanks this is what I need in my function. Because I need the date modified to process my files.
      – Jack Lim
      Nov 8 at 8:24















    up vote
    2
    down vote



    accepted










    Paramiko indeed won't preserve timestamp when transferring files.



    You have to explicitly call the os.utime after the download.





    Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.get() method.



    You can reuse their implementation (code simplified by me):



    sftpattrs = sftp.stat(tempremote)
    os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))




    Similarly for uploads.






    share|improve this answer



















    • 1




      Thanks this is what I need in my function. Because I need the date modified to process my files.
      – Jack Lim
      Nov 8 at 8:24













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Paramiko indeed won't preserve timestamp when transferring files.



    You have to explicitly call the os.utime after the download.





    Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.get() method.



    You can reuse their implementation (code simplified by me):



    sftpattrs = sftp.stat(tempremote)
    os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))




    Similarly for uploads.






    share|improve this answer














    Paramiko indeed won't preserve timestamp when transferring files.



    You have to explicitly call the os.utime after the download.





    Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.get() method.



    You can reuse their implementation (code simplified by me):



    sftpattrs = sftp.stat(tempremote)
    os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))




    Similarly for uploads.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 8 at 8:24

























    answered Nov 8 at 7:40









    Martin Prikryl

    83.3k22154342




    83.3k22154342








    • 1




      Thanks this is what I need in my function. Because I need the date modified to process my files.
      – Jack Lim
      Nov 8 at 8:24














    • 1




      Thanks this is what I need in my function. Because I need the date modified to process my files.
      – Jack Lim
      Nov 8 at 8:24








    1




    1




    Thanks this is what I need in my function. Because I need the date modified to process my files.
    – Jack Lim
    Nov 8 at 8:24




    Thanks this is what I need in my function. Because I need the date modified to process my files.
    – Jack Lim
    Nov 8 at 8:24












    up vote
    1
    down vote













    There doesn't seem to be a way to copy with the stats documented in the paramiko SFTP module. It makes sense why though, because copying the stats besides times for a remote file wouldn't necessarily make sense (i.e. the user/group ids would not make sense on your local machine).



    You can just copy the file, then get the atime/mtime/ctime using the SFTP client's stat or lstat methods and set those on the local file using os.utime.






    share|improve this answer

























      up vote
      1
      down vote













      There doesn't seem to be a way to copy with the stats documented in the paramiko SFTP module. It makes sense why though, because copying the stats besides times for a remote file wouldn't necessarily make sense (i.e. the user/group ids would not make sense on your local machine).



      You can just copy the file, then get the atime/mtime/ctime using the SFTP client's stat or lstat methods and set those on the local file using os.utime.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        There doesn't seem to be a way to copy with the stats documented in the paramiko SFTP module. It makes sense why though, because copying the stats besides times for a remote file wouldn't necessarily make sense (i.e. the user/group ids would not make sense on your local machine).



        You can just copy the file, then get the atime/mtime/ctime using the SFTP client's stat or lstat methods and set those on the local file using os.utime.






        share|improve this answer












        There doesn't seem to be a way to copy with the stats documented in the paramiko SFTP module. It makes sense why though, because copying the stats besides times for a remote file wouldn't necessarily make sense (i.e. the user/group ids would not make sense on your local machine).



        You can just copy the file, then get the atime/mtime/ctime using the SFTP client's stat or lstat methods and set those on the local file using os.utime.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 3:11









        jlucier

        43338




        43338






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200800%2fpython-paramiko-sftp-get-file-along-with-file-timestamp-stat%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()