labels_path = os.path.join(path, '{}-labels.idx1-ubyte' % (kind)) TypeError: not all arguments converted during string formatting
时间: 2024-05-06 07:18:17 浏览: 18
这个错误是因为在字符串格式化时使用了错误的占位符。正确的占位符是`{}`,而不是`%()`。请修改为以下代码:
```
labels_path = os.path.join(path, '{}-labels.idx1-ubyte'.format(kind))
```
或者使用 f-string 格式化字符串:
```
labels_path = os.path.join(path, f'{kind}-labels.idx1-ubyte')
```
相关问题
labels_path = os path join (path, %s-labels-idx1-ubyte % kind)
这是一行代码,使用了Python的os库中的path.join()方法,将字符串"path"和"%s-labels-idx1-ubyte % kind"拼接成一个路径。其中,%s和%kind为占位符,需要在后面传入具体的值进行替换。这行代码的作用是生成MNIST数据集中标签文件的路径。具体的解释如下:
- os.path.join()方法可以将多个路径组合成一个完整的路径。它会根据操作系统的不同自动使用不同的路径分隔符。
- "path"是一个字符串,表示MNIST数据集所在的路径。
- "%s-labels-idx1-ubyte % kind"也是一个字符串,其中%s表示占位符,表示后面需要传入一个字符串进行替换;"-labels-idx1-ubyte"是MNIST数据集中标签文件的固定部分;%kind也是占位符,表示后面需要传入一个字符串进行替换,用于表示是训练集还是测试集。
- 最后,os.path.join()方法将"path"和"%s-labels-idx1-ubyte % kind"拼接成一个完整的路径,表示MNIST数据集中标签文件的路径。
# 将图片移动到相应的文件夹 cluster_path = os.path.join(path, cluster_folder) if not os.path.exists(cluster_path): os.mkdir(cluster_path) for i in range(10): cluster_i_path = os.path.join(cluster_path, str(i)) if not os.path.exists(cluster_i_path): os.mkdir(cluster_i_path) for j, label in enumerate(labels): if label == i: old_path = os.path.join(path, f'{j}.png') new_path = os.path.join(cluster_i_path, f'{j}.png') os.rename(old_path, new_path) root_path = r'D:\jk' for i in range(10): cluster_i_path = os.path.join(root_path, f'cluster_{i}') if not os.path.exists(cluster_i_path): os.mkdir(cluster_i_path) for j, label in enumerate(labels): if label == i: old_path = os.path.join(path, f'{j}.png') new_path = os.path.join(cluster_i_path, f'{j}.png') os.rename(old_path, new_path),这段代码有什么错误吗
这段代码没有语法错误,但是注意到两个循环中的变量 `i` 和 `j` 是一样的,所以在第二个循环中可以使用之前定义的变量 `i`,而不需要重新定义变量。修改后的代码如下:
```
root_path = r'D:\jk'
for i in range(10):
cluster_i_path = os.path.join(root_path, f'cluster_{i}')
if not os.path.exists(cluster_i_path):
os.mkdir(cluster_i_path)
for j, label in enumerate(labels):
if label == i:
old_path = os.path.join(path, f'{j}.png')
new_path = os.path.join(cluster_i_path, f'{j}.png')
os.rename(old_path, new_path)
```
另外,你需要将聚类文件夹的根目录 `root_path` 修改成你想要的路径。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)