)File类中以字符串形式返回文件绝对路径的方法是
时间: 2024-05-17 17:17:11 浏览: 4
File 类中以字符串形式返回文件绝对路径的方法是 getAbsolutePath()。这个方法返回文件的绝对路径,包括文件名。如果文件对象是相对路径,则返回相对于当前工作目录的绝对路径。例如,如果一个文件对象表示的是 "C:\temp\file.txt",那么调用 getAbsolutePath() 方法将返回字符串 "C:\temp\file.txt"。
相关问题
设计函数get_pngs_from_pic_with_binary将pic文件夹中所有*.png文件的绝对路径按utf-8编码以二进制的形式输出到imagepng.dat文件中,每个*.png文件的绝对路径占imagepng.dat文件的一行, 该函数包含两个默认值参数(图像文件夹参数、imagepng.dat的全路径)。编写函数read_png_fullpath_bybinary(), 以字符串列表的形式返回imagepng.dat文件的内容,该函数包含一个默认值参数txtpath, 调用read_png_fullpath_bybinary (),逐行打印imagepng.dat文件的内容。
好的,这是你的第一个问题,以下是对应的函数实现:
```python
import os
def get_pngs_from_pic_with_binary(pic_folder='pic', dat_file='imagepng.dat'):
with open(dat_file, 'wb') as f:
for root, dirs, files in os.walk(pic_folder):
for file in files:
if file.endswith('.png'):
path = os.path.abspath(os.path.join(root, file))
f.write(path.encode('utf-8') + b'\n')
def read_png_fullpath_bybinary(txtpath='imagepng.dat'):
with open(txtpath, 'rb') as f:
content = f.readlines()
content = [c.strip().decode('utf-8') for c in content]
return content
# 打印imagepng.dat文件的内容
def print_imagepng():
content = read_png_fullpath_bybinary()
for line in content:
print(line)
```
可以直接调用`get_pngs_from_pic_with_binary()`函数来将*.png文件的路径以二进制形式写入imagepng.dat文件中。调用`read_png_fullpath_bybinary()`函数来读取imagepng.dat文件,返回一个包含所有路径的字符串列表。最后,调用`print_imagepng()`函数来打印imagepng.dat文件的内容。
python:设计函数get_pngs_from_pic将pic文件夹中所有*.png文件的绝对路径按cp936编码以文本的形式输出到imagepng.txt记事本中,每个*.png文件的绝对路径占imagepng.txt文件的一行,该函数包含两个默认值参数(图像文件夹参数、imagepng.txt的全路径)。编写函数get_pngs_from_bonn将bonn文件夹中*.png文件的全路径追加到imagepng.txt文件的尾部,该函数包含两个默认值参数(图像文件夹参数、imagepng.txt的全路径)。编写函数read_png_fullpath_bytext (), 以字符串列表的形式返回imagepng.txt文件的内容,该函数包含一个默认值参数txtpath, 调用read_png_fullpath_bytext (),逐行打印imagepng.txt文件的内容。
以下是函数的实现:
```python
import os
def get_pngs_from_pic(pic_folder='pic', txt_path='imagepng.txt'):
with open(txt_path, 'w', encoding='cp936') as f:
for root, dirs, files in os.walk(pic_folder):
for file in files:
if file.endswith('.png'):
png_path = os.path.join(root, file)
f.write(png_path + '\n')
def get_pngs_from_bonn(bonn_folder='bonn', txt_path='imagepng.txt'):
with open(txt_path, 'a', encoding='cp936') as f:
for root, dirs, files in os.walk(bonn_folder):
for file in files:
if file.endswith('.png'):
png_path = os.path.join(root, file)
f.write(png_path + '\n')
def read_png_fullpath_bytext(txtpath='imagepng.txt'):
with open(txtpath, 'r', encoding='cp936') as f:
content = f.read().splitlines()
return content
# 打印imagepng.txt文件的内容
for line in read_png_fullpath_bytext():
print(line)
```
这些函数的作用分别为:
- `get_pngs_from_pic`: 将 `pic` 文件夹中所有 `.png` 文件的绝对路径按照 `cp936` 编码写入到 `imagepng.txt` 文件中。
- `get_pngs_from_bonn`: 将 `bonn` 文件夹中所有 `.png` 文件的绝对路径按照 `cp936` 编码追加写入到 `imagepng.txt` 文件中。
- `read_png_fullpath_bytext`: 以字符串列表的形式返回 `imagepng.txt` 文件的内容。
注意:默认情况下,`pic` 和 `bonn` 文件夹应该与代码文件在同一目录下。如果不是,在调用函数时需要传入正确的文件夹路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)