首页 > 解决方案 > 如何确定文件扩展名?

问题描述


我试图复制路径为:userDATA\user1\profile_image 的图像,但不幸的是,我不知道文件扩展名,因为用户可以删除自己的图像,因此后缀有时会有所不同。
file_path = drop_image_instance.image_path   # the path looks like this: C:\user\images\download1.png
copyfile(file_path, "userDATA\\" + username + "\\profile_image")

我的问题是,我怎样才能找出文件有哪个后缀,或者我怎样才能检测到 file_path 的结尾?例如:

def find_suffix(path):
    file_ending = path[1]
    return file_ending

感谢您的回答!

标签: python-3.xfile

解决方案


推荐阅读