skip to Main Content

My part of code is

    js_file = response.text
    filename = [x for x in df['col1']]
    for names in filename:
        with open(names, 'w') as outfile:
           json.dump(js_file,outfile)

What i need to do is to export to specific folder which doesn’t exist and specify the extension of filenames

for an example for the extension:

  • filename1.json
  • filename2.json
  • … so on

I know it’s already exported as json file but i need to know how to add the extension to multiple files and put files in specific folder that doesn’t exist

Your help will be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    This code worked for me for whatever number of rows:

    Lines here is same as rows

    count = 0
    for line in Lines:
        new_line = line.strip()
        response = requests.get(
            f"URL that include new_line"
        )
        for names in Lines:
            count += 1
            js_body = response.text
            json_file_name = os.path.join(directory, names)
            with open(json_file_name, 'w') as outfile:
                json.dump(js_body,outfile)
            done = "Operation successfully completed"
            print(done,": ", count,", Filename: ", json_file_name)
        if count == len(Lines):
            break
    print('finished successfully')
    

  2. The following code can be used

    import pathlib
    import os
    js_file = response.text
    # expecting json filenames present in col1 like " 'filename1', 'filename2' "
    filenames = df['col1']
    parent_folder = "folder_name"
    pathlib.Path(parent_folder).mkdir(parents=True, exist_ok=True)
    for names in filenames:
      json_file_name = os.path.join(parent_folder, names+".json" )
      with open(json_file_name, 'w') as outfile:
         json.dump(js_file,outfile)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search