python 尝试写入 yaml 文件时抛出 NoneType 错误
python throws NoneType error when trying to write to yaml file
我的代码相对简单,但我知道文件搜索有效,但是,将数据写入 YAML 却无法正常工作。我已经通过 here 寻求建议并尝试了 extend()、append() 和 update()。一切都无济于事,任何建议都会有所帮助。
import os
import YAML
drives = ["A:\", "B:\", "C:\", "D:\", "E:\",
"F:\", "G:\", "H:\", "I:\", "J:\", "L:\"]
for drive in drives:
print("Searching in drive: " + f"'{drive}'")
dir_path = drive
for root, dirs, files in os.walk(dir_path):
for file in files:
# change the extension from '.mp3' to
# the one of your choice.
if file.endswith('.exe'):
back = '\'
print("File path: " + f"'{root + back + str(file)}'")
program = file
path = root + back + str(file)
with open('programs.yaml', 'r') as yaml_file:
cur_yaml = yaml.safe_load(yaml_file)
info = program + ": " + path
print("YAML data: " + f"'{info}'")
cur_yaml.append(info)
print("curyaml:" + cur_yaml)
if cur_yaml:
with open('programs.yaml', 'w') as yaml_file:
yaml.safe_dump(cur_yaml, yaml_file)
目标是让它搜索计算机中的所有驱动器并将可执行文件的位置记录在 YAML 中以备后用,目的是用于登录 Steam、个人游戏、程序等。
我不断收到的错误是:
Traceback (most recent call last):
File "I:\HELIX\Modules\Program Finder\search\search.py", line 24, in <module>
cur_yaml.append(info)
AttributeError: 'NoneType' object has no attribute 'append'
Searching in drive: 'A:\'
Searching in drive: 'B:\'
Searching in drive: 'C:\'
File path: 'C:\actions-runner\bin\Runner.Listener.exe'
YAML data: 'Runner.Listener.exe: C:\actions-runner\bin\Runner.Listener.exe'
Process finished with exit code 1
我要指出的最后一件事是我的 YAML 文件是空的,我还没有决定如何为我正在制作的更大程序的这一部分构建数据。
cur_yaml
的类型是 str()
按基本字符拆分。
'''The last thing I can point out is that my YAML file is empty, I have yet to decide how to structure the data for this section of a larger program I am making.'''
在这种情况下 NoneType
对象被加载。
我的代码相对简单,但我知道文件搜索有效,但是,将数据写入 YAML 却无法正常工作。我已经通过 here 寻求建议并尝试了 extend()、append() 和 update()。一切都无济于事,任何建议都会有所帮助。
import os
import YAML
drives = ["A:\", "B:\", "C:\", "D:\", "E:\",
"F:\", "G:\", "H:\", "I:\", "J:\", "L:\"]
for drive in drives:
print("Searching in drive: " + f"'{drive}'")
dir_path = drive
for root, dirs, files in os.walk(dir_path):
for file in files:
# change the extension from '.mp3' to
# the one of your choice.
if file.endswith('.exe'):
back = '\'
print("File path: " + f"'{root + back + str(file)}'")
program = file
path = root + back + str(file)
with open('programs.yaml', 'r') as yaml_file:
cur_yaml = yaml.safe_load(yaml_file)
info = program + ": " + path
print("YAML data: " + f"'{info}'")
cur_yaml.append(info)
print("curyaml:" + cur_yaml)
if cur_yaml:
with open('programs.yaml', 'w') as yaml_file:
yaml.safe_dump(cur_yaml, yaml_file)
目标是让它搜索计算机中的所有驱动器并将可执行文件的位置记录在 YAML 中以备后用,目的是用于登录 Steam、个人游戏、程序等。
我不断收到的错误是:
Traceback (most recent call last):
File "I:\HELIX\Modules\Program Finder\search\search.py", line 24, in <module>
cur_yaml.append(info)
AttributeError: 'NoneType' object has no attribute 'append'
Searching in drive: 'A:\'
Searching in drive: 'B:\'
Searching in drive: 'C:\'
File path: 'C:\actions-runner\bin\Runner.Listener.exe'
YAML data: 'Runner.Listener.exe: C:\actions-runner\bin\Runner.Listener.exe'
Process finished with exit code 1
我要指出的最后一件事是我的 YAML 文件是空的,我还没有决定如何为我正在制作的更大程序的这一部分构建数据。
cur_yaml
的类型是 str()
按基本字符拆分。
'''The last thing I can point out is that my YAML file is empty, I have yet to decide how to structure the data for this section of a larger program I am making.'''
在这种情况下 NoneType
对象被加载。