Last active
August 29, 2015 13:56
-
-
Save frodenas/9107624 to your computer and use it in GitHub Desktop.
Python shutil.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@a28e4835-146b-4da6-b736-1021126986a0:~# uname -a | |
Linux a28e4835-146b-4da6-b736-1021126986a0 3.8.0-35-generic #50-Ubuntu SMP Tue Dec 3 01:24:59 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux | |
root@a28e4835-146b-4da6-b736-1021126986a0:~# python -V | |
Python 2.7.4 | |
root@a28e4835-146b-4da6-b736-1021126986a0:~# python | |
Python 2.7.4 (default, Sep 26 2013, 03:20:26) | |
[GCC 4.7.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import os | |
>>> import pwd | |
>>> import shutil | |
>>> import tempfile | |
>>> | |
>>> pwent = pwd.getpwnam('vcap') | |
>>> (home_dir, uid, gid) = (pwent.pw_dir, pwent.pw_uid, pwent.pw_gid) | |
>>> ssh_dir = os.path.join(home_dir, '.ssh') | |
>>> authorized_keys_file = os.path.join(ssh_dir, 'authorized_keys') | |
>>> os.stat(authorized_keys_file) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
OSError: [Errno 2] No such file or directory: '/home/vcap/.ssh/authorized_keys' | |
>>> | |
>>> with tempfile.NamedTemporaryFile(delete=False) as keys_file: | |
... os.fchmod(keys_file.fileno(), 0600) | |
... os.fchown(keys_file.fileno(), uid, gid) | |
... new_keys_path = keys_file.name | |
... os.stat(new_keys_path) | |
... shutil.move(new_keys_path, authorized_keys_file) | |
... | |
posix.stat_result(st_mode=33152, st_ino=14, st_dev=1792L, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1392874545, st_mtime=1392874545, st_ctime=1392874545) | |
>>> os.stat(authorized_keys_file) | |
posix.stat_result(st_mode=33152, st_ino=140274, st_dev=2049L, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1392874545, st_mtime=1392874545, st_ctime=1392874545) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment