Python 由于目录错误找不到我的文件
Python can't find my file because of a directory error
所以我有一个功能可以抓取 instagram 数据并将喜欢的数量存储到 dictionary.I 认为我已经正确设置了功能但是当我尝试 运行 打印语句时,我不断收到错误消息,提示无法通过函数 运行 找到我要查找的文件。
这是我用来挂载 google 驱动器并设置目录以存储我的 json 文件的代码。我在我的驱动器中验证了这一切都是正确的。
# Mount data location. Do not edit this cell except as indicated.
# Be sure you have a folder in the root of your Google Drive called APRD6342/Data.
# Data files for the course should be uploaded to that folder.
from pathlib import Path
try:
from google.colab import drive
drive.mount('/content/drive')
datadir = Path('drive/My Drive/APRD6342/Data') # you may edit this location ...
except ModuleNotFoundError:
datadir = Path('../../Data') # ... but don't change this!!!
INSTAGRAM_DIR = datadir / 'instagram'
这是我的函数:
def engagement(filename, follower_count):
"""Return the brand post engagement for the Instagram metadata file,
filename, given follower_count as the number of Instagram followers for
the brand.
Returns a decimal engagement rate rounded to 4 decimal places. Python's
standard `round` function should be used. E.g.:
>>> engagement('instagram/volvocars.json', volvocars_follower_count)
0.0125
"""
with open(datadir / filename) as f:
for line in f: #for each line in the file
line_object = json.loads(line)
likes = line_object["GraphImages"]["edge_media_preview_like"]["count"] #find count
if num in likes > 1:
engagement.append(i) #add count to engagement
comments = filename["GraphImages"]["edge_media_to_comment"]["count"] #find other count
if num in comments > 1:
engagement.append(i) #add count
engage_perc = sum(engagement)/follower_count #sum all counts and divide by the number of followers
return engage_perc
这是我尝试 运行 的打印语句和错误:
print('Honda:',
as_percent(engagement(INSTAGRAM_DIR / 'honda.json', honda_follower_count)))
---> 12 with open(datadir / filename) as f:
13 for line in f: #for each line in the file
14 line_object = json.loads(line)
FileNotFoundError: [Errno 2] No such file or directory: 'drive/My Drive/APRD6342/Data/drive/My Drive/APRD6342/Data/instagram/honda.json'
我已经 运行 安装代码之前没问题,但由于某种原因我的目录正在嵌套自身或其他东西。谢谢!
当您使用
调用函数时
print('Honda:',
as_percent(engagement(INSTAGRAM_DIR / 'honda.json', honda_follower_count)))
您提供了完整路径,但随后在函数中再次添加了路径
with open(datadir / filename) as f:
仅传递子文件夹 instagram
和文件名。请注意文档字符串 - 他们提供 'instagram/volvocars.json'
,因此您需要提供 'instagram/honda.json'
因此能够想出一种不必使用 jsonl 文件而只使用 json 的方法。混淆了一些语法。
def engagement(filename, follower_count):
"""Return the brand post engagement for the Instagram metadata file,
filename, given follower_count as the number of Instagram followers for
the brand.
Returns a decimal engagement rate rounded to 4 decimal places. Python's
standard `round` function should be used. E.g.:
>>> engagement('instagram/volvocars.json', volvocars_follower_count)
0.0125
"""
engagement = [] #set empty list for like counts
data = filename #store filename to variable
with open(data) as f: #save open file as outfile
parse = json.load(f)
for n in range(0,200):
likes = parse["GraphImages"][n]["edge_media_preview_like"]["count"] #find count
if likes > 1:
engagement.append(likes) #add count to engagement
comments = parse["GraphImages"][n]["edge_media_to_comment"]["count"] #find other count
if comments > 1:
engagement.append(comments) #add count
engage_perc = (sum(engagement)/len(range(0,200)))/follower_count #sum all counts and divide by the number of followers
return round(engage_perc,4)
所以我有一个功能可以抓取 instagram 数据并将喜欢的数量存储到 dictionary.I 认为我已经正确设置了功能但是当我尝试 运行 打印语句时,我不断收到错误消息,提示无法通过函数 运行 找到我要查找的文件。
这是我用来挂载 google 驱动器并设置目录以存储我的 json 文件的代码。我在我的驱动器中验证了这一切都是正确的。
# Mount data location. Do not edit this cell except as indicated.
# Be sure you have a folder in the root of your Google Drive called APRD6342/Data.
# Data files for the course should be uploaded to that folder.
from pathlib import Path
try:
from google.colab import drive
drive.mount('/content/drive')
datadir = Path('drive/My Drive/APRD6342/Data') # you may edit this location ...
except ModuleNotFoundError:
datadir = Path('../../Data') # ... but don't change this!!!
INSTAGRAM_DIR = datadir / 'instagram'
这是我的函数:
def engagement(filename, follower_count):
"""Return the brand post engagement for the Instagram metadata file,
filename, given follower_count as the number of Instagram followers for
the brand.
Returns a decimal engagement rate rounded to 4 decimal places. Python's
standard `round` function should be used. E.g.:
>>> engagement('instagram/volvocars.json', volvocars_follower_count)
0.0125
"""
with open(datadir / filename) as f:
for line in f: #for each line in the file
line_object = json.loads(line)
likes = line_object["GraphImages"]["edge_media_preview_like"]["count"] #find count
if num in likes > 1:
engagement.append(i) #add count to engagement
comments = filename["GraphImages"]["edge_media_to_comment"]["count"] #find other count
if num in comments > 1:
engagement.append(i) #add count
engage_perc = sum(engagement)/follower_count #sum all counts and divide by the number of followers
return engage_perc
这是我尝试 运行 的打印语句和错误:
print('Honda:',
as_percent(engagement(INSTAGRAM_DIR / 'honda.json', honda_follower_count)))
---> 12 with open(datadir / filename) as f:
13 for line in f: #for each line in the file
14 line_object = json.loads(line)
FileNotFoundError: [Errno 2] No such file or directory: 'drive/My Drive/APRD6342/Data/drive/My Drive/APRD6342/Data/instagram/honda.json'
我已经 运行 安装代码之前没问题,但由于某种原因我的目录正在嵌套自身或其他东西。谢谢!
当您使用
调用函数时print('Honda:',
as_percent(engagement(INSTAGRAM_DIR / 'honda.json', honda_follower_count)))
您提供了完整路径,但随后在函数中再次添加了路径
with open(datadir / filename) as f:
仅传递子文件夹 instagram
和文件名。请注意文档字符串 - 他们提供 'instagram/volvocars.json'
,因此您需要提供 'instagram/honda.json'
因此能够想出一种不必使用 jsonl 文件而只使用 json 的方法。混淆了一些语法。
def engagement(filename, follower_count):
"""Return the brand post engagement for the Instagram metadata file,
filename, given follower_count as the number of Instagram followers for
the brand.
Returns a decimal engagement rate rounded to 4 decimal places. Python's
standard `round` function should be used. E.g.:
>>> engagement('instagram/volvocars.json', volvocars_follower_count)
0.0125
"""
engagement = [] #set empty list for like counts
data = filename #store filename to variable
with open(data) as f: #save open file as outfile
parse = json.load(f)
for n in range(0,200):
likes = parse["GraphImages"][n]["edge_media_preview_like"]["count"] #find count
if likes > 1:
engagement.append(likes) #add count to engagement
comments = parse["GraphImages"][n]["edge_media_to_comment"]["count"] #find other count
if comments > 1:
engagement.append(comments) #add count
engage_perc = (sum(engagement)/len(range(0,200)))/follower_count #sum all counts and divide by the number of followers
return round(engage_perc,4)