Saurabh Verma
Saurabh Verma

Reputation: 6728

Using wildcards in filename in scp in python

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 [email protected]:/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 '[email protected]:/home/test/test2/test3/%s' %s '''

instead of:

command = ''' scp [email protected]:/home/test/test2/test3/%s %s '''\
                          % (filename,self.unprocessed_file_dir)

Upvotes: 0

Views: 1431

Answers (1)

perreal
perreal

Reputation: 98088

This works on my system:

host = '[email protected]'
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 [email protected] ls /home/test/test2/test3/*last_processed_date*.txt

Upvotes: 1

Related Questions