7 năm trước
Cố gắng tạo chức năng được gọi là parseExtension lấy tên tệp làm tham số chuỗi và trả về phần mở rộng tệp. Sẽ yêu cầu người dùng nhập tên tệp IE. Python.exe sau đó in phần mở rộng tệp. Tôi đã cố gắng chia văn bản và một số vấn đề cho tôi cho đến nay. Tôi đang sử dụng 2.7. tức là vui lòng nhập tên tệp của bạn: python.exe tiện ích mở rộng tệp của bạn là một: exe
Going to ask users to input a filename ie. Python.exe then print the file extension.
I tried to split the text and a number of issues for me so far. I'm using 2.7.
i.e. Please enter your filename: Python.exe
Your file extension is an: exe
def parseExtension [tên tệp]:
Câu trả lời được đề xuất
Tốt nhất để sử dụng hệ điều hành mô -đun ...
''' file_splitext.py get a given file's extension ''' import os filename = "myfile.exe" name, extension = os.path.splitext[filename] print["name = {} extension = {}".format[name, extension]] ''' result ... name = myfile extension = .exe '''
Nhảy để đăng
Tất cả 5 câu trả lời
rproffitt2.346 "Không có gì để xem ở đây." Người điều hành 2,346 "Nothing to see here."
Moderator Được chỉnh sửa 7 năm trước bởi rproffitt
rproffitt2.346 "Không có gì để xem ở đây." Người điều hành 2,346 "Nothing to see here."
Moderator
7 năm trước
Được chỉnh sửa 7 năm trước bởi rproffitt
if filename.strip['any'][-3:]: #Check if the filename no matter what it is report back 3 spots from the period.
print filename[-3:] #Print the filename 3 spaces from 0 to the right.
filename.split['.'] #Split the filename from the 'period'.
Ya tôi đã thấy rằng Ty, tôi đã không sử dụng GUI và cần nó với một chức năng nhưng tôi đã thấy điều đó trước đây và phát hiện ra tôi đã làm như thế nào như thế này: 1,735 DaniWeb's Hypocrite Team Colleague
7 năm trướcTốt nhất để sử dụng hệ điều hành mô -đun ...
''' file_splitext.py
get a given file's extension
'''
import os
filename = "myfile.exe"
name, extension = os.path.splitext[filename]
print["name = {} extension = {}".format[name, extension]]
''' result ...
name = myfile extension = .exe
'''
7 năm trước
Nhảy để đăng
Tất cả 5 câu trả lời
rproffitt2.346 "Không có gì để xem ở đây." Người điều hành
Được chỉnh sửa 7 năm trước bởi rproffitt
Ở đây, điều này sẽ bao gồm tất cả các căn cứ. Nó xử lý tất cả các loại vấn đề cho bạn, bao gồm [nhưng không giới hạn quá] thay thế ký tự.
Hoạt động trong Windows, *Nix và hầu hết mọi hệ thống tệp khác. Chỉ cho phép các ký tự có thể in.
def txt2filename[txt, chr_set='normal']:
"""Converts txt to a valid Windows/*nix filename with printable characters only.
args:
txt: The str to convert.
chr_set: 'normal', 'universal', or 'inclusive'.
'universal': ' -.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
'normal': Every printable character exept those disallowed on Windows/*nix.
'extended': All 'normal' characters plus the extended character ASCII codes 128-255
"""
FILLER = '-'
# Step 1: Remove excluded characters.
if chr_set == 'universal':
# Lookups in a set are O[n] vs O[n * x] for a str.
printables = set[' -.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz']
else:
if chr_set == 'normal':
max_chr = 127
elif chr_set == 'extended':
max_chr = 256
else:
raise ValueError[f'The chr_set argument may be normal, extended or universal; not {chr_set=}']
EXCLUDED_CHRS = set[r':"/\|?*'] # Illegal characters in Windows filenames.
EXCLUDED_CHRS.update[chr[127]] # DEL [non-printable].
printables = set[chr[x]
for x in range[32, max_chr]
if chr[x] not in EXCLUDED_CHRS]
result = ''.join[x if x in printables else FILLER # Allow printable characters only.
for x in txt]
# Step 2: Device names, '.', and '..' are invalid filenames in Windows.
DEVICE_NAMES = 'CON,PRN,AUX,NUL,COM1,COM2,COM3,COM4,' \
'COM5,COM6,COM7,COM8,COM9,LPT1,LPT2,' \
'LPT3,LPT4,LPT5,LPT6,LPT7,LPT8,LPT9,' \
'CONIN$,CONOUT$,..,.'.split[] # This list is an O[n] operation.
if result in DEVICE_NAMES:
result = f'-{result}-'
# Step 3: Maximum length of filename is 255 bytes in Windows and Linux [other *nix flavors may allow longer names].
result = result[:255]
# Step 4: Windows does not allow filenames to end with '.' or ' ' or begin with ' '.
result = re.sub[r'^[. ]', FILLER, result]
result = re.sub[r' $', FILLER, result]
return result
Giải pháp này không cần thư viện bên ngoài. Nó thay thế các tên tệp không thể in cũng vì chúng không phải lúc nào cũng đơn giản để đối phó.