python输出文件夹中png文件的路径
时间: 2024-03-23 20:43:30 浏览: 45
可以使用 Python 中的 os 模块和 glob 模块来实现这个功能。具体代码如下:
```python
import os
import glob
# 定义文件夹路径
folder_path = '/path/to/folder'
# 查找所有的 PNG 文件
png_files = glob.glob(os.path.join(folder_path, '*.png'))
# 输出每个 PNG 文件的路径
for png_file in png_files:
print(png_file)
```
在代码中,我们首先定义了一个文件夹路径 `folder_path`,然后使用 `glob.glob()` 函数查找该文件夹下所有的 PNG 文件。最后,使用循环输出每个 PNG 文件的路径。
相关问题
python将文件夹中png文件的绝对路径按cp936编码以文本形式输出到记事本中
可以使用Python的os和codecs库来实现。具体代码如下:
```python
import os
import codecs
# 文件夹路径,该路径下包含png文件
folder_path = 'your/folder/path'
# 获取文件夹中所有png文件的绝对路径
file_paths = [os.path.abspath(os.path.join(folder_path, f)) for f in os.listdir(folder_path) if f.endswith('.png')]
# 以cp936编码方式将文件路径写入文本文件
with codecs.open('file_paths.txt', 'w', 'cp936') as f:
f.write('\n'.join(file_paths))
```
将以上代码中的`your/folder/path`替换为你的文件夹路径,即可将该文件夹中所有png文件的绝对路径按cp936编码输出到名为`file_paths.txt`的文本文件中。
python读取文件夹下png图片长和宽
你可以使用Python的os和Pillow库来读取文件夹下的PNG图片,并获取它们的长和宽。下面是一个例子:
```python
import os
from PIL import Image
# 定义文件夹路径
folder_path = 'path/to/folder'
# 遍历文件夹中的所有PNG文件
for filename in os.listdir(folder_path):
if filename.endswith('.png'):
# 打开PNG图片
img = Image.open(os.path.join(folder_path, filename))
# 获取图片大小(长和宽)
width, height = img.size
# 输出图片大小
print(filename + ' 的宽度为:', width)
print(filename + ' 的高度为:', height)
```
需要注意的是,这个例子中的`path/to/folder`应该替换成你实际的文件夹路径。同时,如果文件夹中有其他类型的文件(例如JPG文件),你可能需要修改代码中的判断条件。
阅读全文
相关推荐
















