Using wildcards in filename in scp in python

无人久伴 提交于 2019-12-12 04:05:49

问题


I want to execute a simple scp command in a python script, copying files following a certain name pattern.

I'm executing the following command:

filename = '\*last_processed_date\*.txt'
command = ''' scp test@100.41.14.27:/home/test/test2/test3/%s %s '''\
                      % (filename,self.unprocessed_file_dir)
os.system(command)

I understand that I have to escape the wildcard '*', which I'm doing..but still I get:

scp: /home/test/test2/test3/*last_processed_date*.txt: No such file or directory

I'm wondering what I'm doing wrong..

EDIT: It was a careless mistake from my side. I should have done:

command = ''' scp 'test@100.41.14.27:/home/test/test2/test3/%s' %s '''

instead of:

command = ''' scp test@100.41.14.27:/home/test/test2/test3/%s %s '''\
                          % (filename,self.unprocessed_file_dir)

回答1:


This works on my system:

host = 'test@100.41.14.27'
filename = '*last_processed_date*.txt'
rpath = '/home/test/test2/test3'
lpath = self.unprocessed_file_dir
command = 'scp %s:%s/%s %s' % (host, rpath, filename, lpath)
os.system(command)

If it gives you an error, try this on from the terminal first:

ssh test@100.41.14.27 ls /home/test/test2/test3/*last_processed_date*.txt


来源:https://stackoverflow.com/questions/23076874/using-wildcards-in-filename-in-scp-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!