简体   繁体   English

如何使用 Python 脚本将某些类型的文件移动到其他目录

[英]How to move certain types of files to other directory with a Python script

My code is:我的代码是:

subprocess.call(["mv", "*.fasta", f"{my_model_dir}/."])

And the error code I got was:我得到的错误代码是:

mv: cannot stat '*.fasta': No such file or directory

I tried moving all the fasta files to other directory, but, I got an error message.我尝试将所有 fasta 文件移动到其他目录,但是,我收到一条错误消息。

I think it's because the '*' is used as a regular expression in Python, so it's not working in this situation.我认为这是因为 '*' 在 Python 中用作正则表达式,所以在这种情况下它不起作用。 To move files with a Python script to other directory, what should I do?要将带有 Python 脚本的文件移动到其他目录,我该怎么办?

try subprocess.call(["mv", "\*.fasta", f"{my_model_dir}/."])尝试subprocess.call(["mv", "\*.fasta", f"{my_model_dir}/."])

You can use the os or shutil library: ( How to move a file in Python? )您可以使用osshutil库:( 如何在 Python 中移动文件?
As for the files of the same type, you can cycle through them using os and then move them one by one.对于同类型的文件,可以用os循环浏览,然后一个一个移动。
os reference: ( https://www.geeksforgeeks.org/os-module-python-examples/ ) os参考:( https://www.geeksforgeeks.org/os-module-python-examples/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM