2010-08-04 31 views
5

Tôi muốn xóa tất cả các tệp trong thư mục đã cho trên máy chủ từ xa mà tôi đã kết nối với sử dụng paramiko. Mặc dù vậy, tôi không thể cung cấp tên tệp một cách rõ ràng, vì chúng sẽ khác nhau tùy thuộc vào phiên bản của tệp mà trước đây tôi đã đặt ở đó.Làm thế nào để xóa tất cả các tệp trong thư mục trên máy chủ từ xa trong python?

Dưới đây là những gì tôi đang cố gắng để làm ... dòng dưới #TODO là tiếng gọi tôi đang cố gắng nơi remoteArtifactPath là một cái gì đó giống như "/ opt/foo/*"

ssh = paramiko.SSHClient() 
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))) 
ssh.connect(server, username=username, pkey=mykey) 
sftp = ssh.open_sftp() 

# TODO: Need to somehow delete all files in remoteArtifactPath remotely 
sftp.remove(remoteArtifactPath+"*") 

# Close to end 
sftp.close() 
ssh.close() 

ý tưởng Bất kỳ làm thế nào tôi có thể đạt được điều này?

Cảm ơn

Trả lời

7

Tôi tìm thấy một giải pháp: Lặp lại trên tất cả các tập tin trong vị trí từ xa, sau đó gọi remove trên mỗi trong số họ:

ssh = paramiko.SSHClient() 
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))) 
ssh.connect(server, username=username, pkey=mykey) 
sftp = ssh.open_sftp() 

# Updated code below: 
filesInRemoteArtifacts = sftp.listdir(path=remoteArtifactPath) 
for file in filesInRemoteArtifacts: 
    sftp.remove(remoteArtifactPath+file) 

# Close to end 
sftp.close() 
ssh.close() 
+3

tôi đề nghị sử dụng '' 'os.path.join (remoteArtifactPath, tập tin)' '' thay vì '' 'sftp.remove (remoteArtifactPath + tập tin)' '', vì '' 'os.path. join() '' 'là nền tảng độc lập. Bộ tách dòng có thể khác nhau theo nền tảng và sử dụng os.path.join đảm bảo đường dẫn được tạo chính xác, bất kể nền tảng. – 9monkeys

8

Một thói quen Fabric có thể đơn giản như thế này:

with cd(remoteArtifactPath): 
    run("rm *") 

Vải rất phù hợp để thực thi lệnh vỏ trên máy chủ từ xa. Vải thực sự sử dụng Paramiko bên dưới, vì vậy bạn có thể sử dụng cả hai nếu bạn cần.

+0

cảm ơn, tôi sẽ xem xét điều đó – Cuga

+0

+1, vì trong EC2 hình ảnh hệ điều hành của chúng tôi được mặc định với SFTP bị tắt. (Tôi không chắc chắn đó là mặc định của Amazon hay công ty của tôi, nhưng câu hỏi là không liên quan bởi vì tôi không thể thay đổi điều đó. Tuy nhiên, tôi vẫn cần xóa tệp đó. –

2

Bạn cần một quy trình đệ quy vì thư mục từ xa của bạn có thể có các thư mục con.

def rmtree(sftp, remotepath, level=0): 
    for f in sftp.listdir_attr(remotepath): 
     rpath = posixpath.join(remotepath, f.filename) 
     if stat.S_ISDIR(f.st_mode): 
      rmtree(sftp, rpath, level=(level + 1)) 
     else: 
      rpath = posixpath.join(remotepath, f.filename) 
      print('removing %s%s' % (' ' * level, rpath)) 
      sftp.remove(rpath) 
    print('removing %s%s' % (' ' * level, remotepath)) 
    sftp.rmdir(remotepath) 

ssh = paramiko.SSHClient() 
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))) 
ssh.connect(server, username=username, pkey=mykey) 
sftp = ssh.open_sftp() 
rmtree(sftp, remoteArtifactPath) 

# Close to end 
stfp.close() 
ssh.close() 
Các vấn đề liên quan