所以我正在撰寫一個腳本來獲取 .txt 檔案和
- 附加每一行的字串表示
- 在檔案的開頭和結尾附加一個字串。
所以
嘿,
我的名字是 Jim,我很高興開始為 Microsoft 作業。我相信我的作業經歷非常適合微軟文化。
謝謝!吉姆
會成為
您好,\n 我叫 Jim,很高興開始為 Microsoft 作業。我相信我的作業經歷非常適合微軟文化。\n謝謝!\n吉姆
同時還將以下內容附加到開頭
{"prompt":"", "completion":"
以及最后的以下內容
"}
現在我有以下腳本成功占用 2 .txt 并創建字串表示并在前端和末尾附加所需的鍵,但它不能超過 2 PLUS 得到以下錯誤
PermissionError: [Errno 13] Permission denied
劇本
import sys
import os
import json
import os
if len(sys.argv) != 2:
print("Please add a directory")
sys.exit(0)
directory = sys.argv[1]
new_dir = os.path.abspath(directory "/script_output")
if not os.path.isdir(new_dir):
os.mkdir(new_dir)
for f_str in os.listdir(directory):
if os.path.isdir(os.path.abspath(directory f_str)):
continue
json_data = {}
json_data["prompt"] = ""
f_abs = os.path.abspath(os.path.join(directory, f_str))
og_file = new_dir "/" f_str
json_file = new_dir "/" os.path.splitext(f_str)[0] ".json"
f = open(f_abs, "r", encoding="utf-8")
new_txt = repr(f.read())[1:-1]
json_data["completion"] = new_txt
with open(json_file, "w") as output_file:
json.dump(json_data, output_file)
print("Done")

編輯 1:
As you can see, I have 2 text files that should turn into Json files within the script_output folder. When I run the comment code, there are no errors but the files don't appear.

uj5u.com熱心網友回復:
問題很明顯。
在您的腳本運行的背景關系中的用戶對檔案沒有讀取權限:C:\Users\Antonio\Desktop\Rank\script_output
它是否作為批處理作業運行,所以是不同的用戶?
另一個問題可能相關。打開該檔案后,您并未關閉該檔案。您正在回圈打開輸入檔案。
還有這條線應該做什么?因為它什么都不做
if os.path.isdir(os.path.abspath(directory f_str)):
continue
有人注意到您為輸出檔案使用了背景關系管理器:
with open(json_file, "w") as output_file:
同樣你應該使用
with open(f_abs, 'r', encoding="utf-8") as input_file:
uj5u.com熱心網友回復:
我認為問題在于您沒有使用正確的目錄來加載檔案。我假設您要加載2.txt存盤在/script_output/檔案夾中的檔案(類似的東西)。
嘗試從您擁有script_output檔案夾的位置運行該檔案,然后像python stack.py .,我.用于當前目錄并命名我的腳本作為stack.py說明示例。
請嘗試使用以下腳本:
import sys
import os
import json
import os
if len(sys.argv) != 2:
print("Please add a directory")
sys.exit(0)
directory = sys.argv[1]
new_dir = os.path.abspath(directory "/script_output")
if not os.path.isdir(new_dir):
os.mkdir(new_dir)
for f_str in os.listdir(new_dir):
if os.path.isdir(os.path.abspath(new_dir f_str)):
continue
json_data = {}
json_data["prompt"] = ""
f_abs = os.path.abspath(os.path.join(new_dir, f_str))
og_file = new_dir "/" f_str
json_file = new_dir "/" os.path.splitext(f_str)[0] ".json"
f = open(f_abs, "r", encoding="utf-8")
f = open(f_abs, "r")
new_txt = repr(f.read())[1:-1]
json_data["completion"] = new_txt
with open(json_file, "w") as output_file:
json.dump(json_data, output_file)
uj5u.com熱心網友回復:
我無法測驗它,但對我來說所有的問題是你創建路徑使用 而不是os.path.join()
當您檢查isdir()您使用directory f_str它創建的字串directoryscript_output代替directory/script_output,然后isdir()回傳False,之后再嘗試運行open("directory/script_output")-因此它試圖打開的目錄-這讓你的錯誤。
如果你使用,你就不會遇到這個問題 os.path.join()
我的版本(有其他變化):
我還檢查擴展名.txt以確保我讀取了正確的檔案。
我使用更具可讀性的變數。
import os
import sys
import json
#directory = sys.argv[1]
directory = "test"
input_dir = os.path.abspath(directory) # later I will no need `abspath()`
output_dir = os.path.join(input_dir, "script_output")
os.makedirs(output_dir, exist_ok=True) # doesn't need `if`
for name in os.listdir(input_dir):
input_path = os.path.join(input_dir, name)
#print('input_path:', input_path)
if os.path.isdir(input_path):
print('dir:', input_path)
continue
basename, ext = os.path.splitext(name)
if ext != '.txt':
print('wrong ext:', input_path)
continue
print('>>> processing:', input_path)
json_data = {}
json_data["prompt"] = ""
json_file = os.path.join(output_dir, basename ".json")
with open(input_path, "r", encoding="utf-8") as f:
json_data["completion"] = f.read()[1:-1]
with open(json_file, "w", encoding="utf-8") as f:
json.dump(json_data, f)
print("Done")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382739.html
