更改Python中的文件扩展名

发布于 2024-09-02 08:46:49 字数 150 浏览 10 评论 0原文

假设使用 CGI 从 index.py 中,我有发布文件 foo.fasta 来显示文件。我想在显示文件中将 foo.fasta 的文件扩展名更改为 foo.aln 。我该怎么做呢?

Suppose from index.py with CGI, I have post file foo.fasta to display file. I want to change foo.fasta's file extension to be foo.aln in display file. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

花伊自在美 2024-09-09 08:46:49

使用 pathlib.Path 的优雅方式:

from pathlib import Path
p = Path('mysequence.fasta')
p.rename(p.with_suffix('.aln'))

An elegant way using pathlib.Path:

from pathlib import Path
p = Path('mysequence.fasta')
p.rename(p.with_suffix('.aln'))
幽蝶幻影 2024-09-09 08:46:49

os.path.splitext()< /a>, os.rename()

例如:

# renamee is the file getting renamed, pre is the part of file name before extension and ext is current extension
pre, ext = os.path.splitext(renamee)
os.rename(renamee, pre + new_extension)

os.path.splitext(), os.rename()

for example:

# renamee is the file getting renamed, pre is the part of file name before extension and ext is current extension
pre, ext = os.path.splitext(renamee)
os.rename(renamee, pre + new_extension)
月竹挽风 2024-09-09 08:46:49
import os
thisFile = "mysequence.fasta"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".aln")

其中 thisFile = 您要更改的文件的绝对路径

import os
thisFile = "mysequence.fasta"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".aln")

Where thisFile = the absolute path of the file you are changing

素手挽清风 2024-09-09 08:46:49

从 Python 3.4 开始,有 pathlib 内置库。所以代码可能是这样的:

from pathlib import Path

filename = "mysequence.fasta"
new_filename = Path(filename).stem + ".aln"

https://docs.python .org/3.4/library/pathlib.html#pathlib.PurePath.stem

我喜欢 pathlib :)

Starting from Python 3.4 there's pathlib built-in library. So the code could be something like:

from pathlib import Path

filename = "mysequence.fasta"
new_filename = Path(filename).stem + ".aln"

https://docs.python.org/3.4/library/pathlib.html#pathlib.PurePath.stem

I love pathlib :)

舞袖。长 2024-09-09 08:46:49

使用这个:

os.path.splitext("name.fasta")[0]+".aln"

下面是上面的工作原理:

splitext 方法将名称与创建元组的扩展分开:

os.path.splitext("name.fasta")

创建的元组现在包含字符串“name”和“fasta”。
然后您只需要访问字符串“name”,它是元组的第一个元素:

os.path.splitext("name.fasta")[0]

然后您想为该名称添加新的扩展名:

os.path.splitext("name.fasta")[0]+".aln"

Use this:

os.path.splitext("name.fasta")[0]+".aln"

And here is how the above works:

The splitext method separates the name from the extension creating a tuple:

os.path.splitext("name.fasta")

the created tuple now contains the strings "name" and "fasta".
Then you need to access only the string "name" which is the first element of the tuple:

os.path.splitext("name.fasta")[0]

And then you want to add a new extension to that name:

os.path.splitext("name.fasta")[0]+".aln"
故乡的云 2024-09-09 08:46:49

正如 AnaPana 提到的,pathlib 在 python 3.4 中更加新、更容易,并且有新的 with_suffix 方法可以轻松处理这个问题:

from pathlib import Path
new_filename = Path(mysequence.fasta).with_suffix('.aln')

As AnaPana mentioned pathlib is more new and easier in python 3.4 and there is new with_suffix method that can handle this problem easily:

from pathlib import Path
new_filename = Path(mysequence.fasta).with_suffix('.aln')
差↓一点笑了 2024-09-09 08:46:49

使用 pathlib 并保留完整路径:

from pathlib import Path
p = Path('/User/my/path')
new_p = Path(p.parent.as_posix() + '/' + p.stem + '.aln')

Using pathlib and preserving full path:

from pathlib import Path
p = Path('/User/my/path')
new_p = Path(p.parent.as_posix() + '/' + p.stem + '.aln')
你是暖光i 2024-09-09 08:46:49

遗憾的是,我遇到了文件名上有多个点的情况,分割文本效果不佳......我的解决方法:

file = r'C:\Docs\file.2020.1.1.xls'
ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
filefinal = file.replace(ext,'')
filefinal = file + '.zip'
os.rename(file ,filefinal)

Sadly, I experienced a case of multiple dots on file name that splittext does not worked well... my work around:

file = r'C:\Docs\file.2020.1.1.xls'
ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
filefinal = file.replace(ext,'')
filefinal = file + '.zip'
os.rename(file ,filefinal)
陪你到最终 2024-09-09 08:46:49
>> file = r'C:\Docs\file.2020.1.1.xls'
>> ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
>> filefinal = file.replace(ext,'.zip')
>> os.rename(file ,filefinal) 

重复扩展的逻辑错误,示例:'C:\Docs\.xls_aaa.xls.xls'

>> file = r'C:\Docs\file.2020.1.1.xls'
>> ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
>> filefinal = file.replace(ext,'.zip')
>> os.rename(file ,filefinal) 

Bad logic for repeating extension, sample: 'C:\Docs\.xls_aaa.xls.xls'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文