119
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Giả sử từ
# 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]
1 với CGI, tôi có tệp bài # 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]
2 để hiển thị tệp. Tôi muốn thay đổi phần mở rộng tệp của ____ 12 thành # 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]
4 trong tệp hiển thị. Tôi làm nó như thế nào?
cs95
349K87 Huy hiệu vàng642 Huy hiệu bạc691 Huy hiệu Đồng87 gold badges642 silver badges691 bronze badges
Khi được hỏi ngày 24 tháng 5 năm 2010 lúc 20:19May 24, 2010 at 20:19
1
Một cách thanh lịch bằng cách sử dụng pathlib.path:
from pathlib import Path
p = Path['mysequence.fasta']
p.rename[p.with_suffix['.aln']]
Đã trả lời ngày 28 tháng 11 năm 2017 lúc 9:42Nov 28, 2017 at 9:42
Nikita Malyavinnikita MalyavinNikita Malyavin
1.6801 Huy hiệu vàng11 Huy hiệu bạc10 Huy hiệu đồng1 gold badge11 silver badges10 bronze badges
4
# 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]
5, # 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]
6Ví dụ:
# 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]
Đã trả lời ngày 24 tháng 5 năm 2010 lúc 20:23May 24, 2010 at 20:23
5
import os
thisFile = "mysequence.fasta"
base = os.path.splitext[thisFile][0]
os.rename[thisFile, base + ".aln"]
Trong đó ThisFile = đường dẫn tuyệt đối của tệp bạn đang thay đổi
Amarillion
23.8K15 Huy hiệu vàng66 Huy hiệu bạc80 Huy hiệu Đồng15 gold badges66 silver badges80 bronze badges
Đã trả lời ngày 9 tháng 9 năm 2011 lúc 14:14Sep 9, 2011 at 14:14
FrydayfrydayFryDay
1.0271 Huy hiệu vàng7 Huy hiệu bạc1 gold badge7 silver badges10 bronze badges
2
Dùng cái này:
os.path.splitext["name.fasta"][0]+".aln"
Và đây là cách các hoạt động trên:
Phương thức SplitExt phân tách tên với phần mở rộng tạo Tuple:
os.path.splitext["name.fasta"]
Tuple được tạo bây giờ chứa các chuỗi "Tên" và "Fasta". Sau đó, bạn chỉ cần truy cập vào chuỗi "Tên" chuỗi là yếu tố đầu tiên của bộ tuple:
os.path.splitext["name.fasta"][0]
Và sau đó bạn muốn thêm một tiện ích mở rộng mới vào tên đó:
os.path.splitext["name.fasta"][0]+".aln"
Đã trả lời ngày 11 tháng 2 năm 2015 lúc 15:02Feb 11, 2015 at 15:02
Multigoodversemultigoodversemultigoodverse
6.95816 Huy hiệu vàng60 Huy hiệu bạc100 Huy hiệu Đồng16 gold badges60 silver badges100 bronze badges
Khi Pathlib được đề cập Anapana mới và dễ dàng hơn trong Python 3.4 và có phương pháp mới với_suffix có thể xử lý vấn đề này một cách dễ dàng:
from pathlib import Path
new_filename = Path[mysequence.fasta].with_suffix['.aln']
Đã trả lời ngày 13 tháng 5 năm 2020 lúc 10:23May 13, 2020 at 10:23
Sử dụng Pathlib và bảo tồn đường dẫn đầy đủ:
from pathlib import Path
p = Path['/User/my/path']
new_p = Path[p.parent.as_posix[] + '/' + p.stem + '.aln']
Đã trả lời ngày 20 tháng 8 năm 2018 lúc 18:13Aug 20, 2018 at 18:13
PollpennpollpennPollPenn
7031 Huy hiệu vàng7 Huy hiệu bạc16 Huy hiệu đồng1 gold badge7 silver badges16 bronze badges
1
Đáng buồn thay, tôi đã trải qua một trường hợp nhiều dấu chấm trên tên tệp mà splittext không hoạt động tốt ... công việc của tôi xung quanh:
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]
Đã trả lời ngày 4 tháng 8 năm 2020 lúc 13:16Aug 4, 2020 at 13:16
Hugo vareshugo varesHugo Vares
8777 Huy hiệu bạc7 Huy hiệu Đồng7 silver badges7 bronze badges
# 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]
0Logic xấu cho phần mở rộng lặp lại, mẫu: 'C: \ docs \ .xls_aaa.xls.xls'
Đã trả lời ngày 10 tháng 8 năm 2020 lúc 23:20Aug 10, 2020 at 23:20
UdevudevuDev
Phù hiệu bằng đồng 2133 bronze badges