[英]Writing to files from jupyter notebook
我試圖運行這段代碼:
from tqdm.auto import tqdm
import os
from datasets import load_dataset
dataset = load_dataset('oscar', 'unshuffled_deduplicated_ar', split='train[:25%]')
text_data = []
file_count = 0
for sample in tqdm(dataset['train']):
sample = sample['text'].replace('\n', ' ')
text_data.append(sample)
if len(text_data) == 10_000:
# once we git the 10K mark, save to file
filename = f'/data/text/oscar_ar/text_{file_count}.txt'
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', encoding='utf-8') as fp:
fp.write('\n'.join(text_data))
text_data = []
file_count += 1
# after saving in 10K chunks, we will have ~2082 leftover samples, we save those now too
with open(f'data/text/oscar_ar/text_{file_count}.txt', 'w', encoding='utf-8') as fp:
fp.write('\n'.join(text_data))
我得到以下 PermissionError:
我嘗試更改此目錄的權限並使用 sudo 特權運行 jupyter,但它仍然無法正常工作。
您正在打開:
with open(f'data/text/oscar_ar/text_{file_count}.txt')
但是你在寫:
filename = f'/Dane/text/oscar_ar/text_{file_count}.txt'
你的截圖說:
filename = f'/date/text/oscar_ar/text_{file_count}.txt'
您必須在data
、 /date
或/Dane
之間做出選擇 :)
此外,您似乎應該刪除/data/text/oscar_ar/text_{file_count}.txt
中的第一個/
。
說明:當您在路徑的開頭放置一個斜杠 ( /
) 時,這意味着從文件系統的根目錄(即頂層)開始查找。 如果你不放斜線,它將從你的當前目錄開始查找。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.