English 中文(简体)
SFTP 一组文件的完整文件路径
原标题:SFTP full file path for a set of files
  • 时间:2012-05-23 15:10:47
  •  标签:
  • python
  • sftp

这么说吧,我有一个文件清单,例如

files = ["C:\MyDir\some_file.txt",
         "C:\MyDir\another_file.txt",
         "C:\MyDir\some_file.old"]

我想取出以“某些”开头的“.txt”文件。 我使用标准fnmatch. filter 方法:

my_files = fnmatch.filter([os.path.basename(i) for i in files], "some*.txt")

哪个返回 [“ some_ file.txt”] 。 现在让我们假设这些文件实际上在 SFTP 网站上, 我想在过滤后从 SFTP 网站上下载它们。 我要如何为想要下载的文件获取完整的文件路径?

os.path.abspath("some_file.txt") 

我可以用另一个通配符( “ * ” ) 来预设我的过滤图案,但这只是一种变通办法。是否有一种干净的方法可以做到这一点?

最佳回答

如果您想要完整路径名称, 那么您就必须在完整路径名上应用过滤器。 < code> fnmatch 模式稍有限制, 并且这些模式无论如何都被转换为正反正反正反正, 所以为什么不直接使用 < code> re :

import re
files = ["C:\MyDir\some_file.txt",
     "C:\MyDir\another_file.txt",
     "C:\MyDir\some_file.old"]

patern = re.compile(r"\some[^\]*.txt$", re.I)
filtered_files = [f for f in files if pattern.search(f)]
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签