Home > Mobile >  Json Script Won't Properly Run
Json Script Won't Properly Run

Time:12-12

So I'm writing a script to take a .txt file and

  1. Attach string representation of each line
  2. Attach a string in the beginning and end of the document.

So

Hey there,

My name is Jim and I'm excited to begin working for Microsoft. I believe my job history fits perfectly within the Microsoft Culture.

Thanks! Jim

Would become

Hey there,\n My name is Jim and I'm excited to begin working for Microsoft. I believe my job history fits perfectly within the Micorosoft Culture. \nThanks! \nJim

While also attaching the following to the beginning

{"prompt":"", "completion":"

and the following at the end

"}

Now I have the following script that successfully takes up to 2 .txt and creates the string representation and attaches the needed keys in the front and end, but it can't do more than 2 PLUS gets the following error

PermissionError: [Errno 13] Permission denied

The Script

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")

Here you can see the full error

CodePudding user response:

The problem is pretty clear.

The user in who's context your script is running doesn't have read permission on file: C:\Users\Antonio\Desktop\Rank\script_output

Is it running as a batch job so a different user?

Another problem maybe related. You are not closing that file after opening it. And you are opening the input file in a loop.

Also what should this line do? Because it does nothing

if os.path.isdir(os.path.abspath(directory f_str)):
    continue

One notices you use a context manager for the output file:

with open(json_file, "w") as output_file:

Similarly you should use

with open(f_abs, 'r', encoding="utf-8") as input_file:

CodePudding user response:

I think the problem is you are not using the correct directory to load the file from. I am assuming you want to load a file 2.txt (something like that) which is stored inside /script_output/ folder.

Try to run the file from the place where you have script_output folder and run like python stack.py ., I use . for the current directory and named my script as stack.py to illustrate an example.

Please try using the following script:

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)
  • Related