在Python中使用scp中的文件名通配符

0 投票
1 回答
1397 浏览
提问于 2025-04-18 02:47

我想在一个Python脚本里执行一个简单的scp命令,按照特定的文件名模式来复制文件。

我正在执行以下命令:

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)

我知道我需要对通配符'*'进行转义,我也这么做了……但是我还是得到了:

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

我在想我哪里做错了……

编辑:
这是我粗心大意造成的错误。我应该这样做:

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

而不是:

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

1 个回答

1

在我的系统上,这个可以正常运行:

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)

如果你遇到错误,先在终端里试试这个:

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

撰写回答