简体   繁体   English

Python - 打印文件名显示的完整路径,而不仅仅是文件名

[英]Python - Printing file name show's full path instead of just the file name

I have some code that prints the file name name however it prints out the whole file path instead of just the name我有一些代码可以打印文件名,但是它会打印出整个文件路径而不仅仅是名称

Here is what it prints now:这是它现在打印的内容:

C:\Users\edward\OneDrive\Pics\X00DL0027.jpg 

Here is what I want这就是我想要的

X00DL0027.jpg

Here is the script:这是脚本:

import json
from PIL import Image
from PIL.ExifTags import TAGS
import os
import os.path
import PIL
from pandas import json_normalize

PIL.Image.MAX_IMAGE_PIXELS = 384000000
rootdir = r"C:\Users\edward\OneDrive\Pics"

newfile = newfile = open('meta.txt', 'w')
newfile.write("Filename                                     |  Image DPI                    | Image Height                  |   Image Width                 |   Image Format                |   Image Mode                  |   Image Frames                |\n")
for file in os.listdir(rootdir):
    # read the image data using PIL
    image = Image.open(os.path.join(rootdir, file))

# extract other basic metadata
info_dict = {
    "Filename": image.filename,
    "Image DPI": image.info['dpi'],
    "Image Height": image.height,
    "Image Width": image.width,
    "Image Format": image.format,
    "Image Mode": image.mode,
    "Frames in Image": getattr(image, "n_frames", 1)
}

line = ""
for label, value in info_dict.items():
    line += f"|{str(value):<30} "  
line += " |"  
newfile.write(line + '\n')

I am not sure what to change within the script.我不确定要在脚本中更改什么。

Any help would be great任何帮助都会很棒

使用这个: "Filename": os.path.basename(image.filename),

你可以使用下面的代码

"Filename": image.filename.split('\')[-1]

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

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