-1

I am trying to check if folder exist if not the system create it and a JSON file will be written inside this folder.

The problem is that the system create an empty folder and displays this error:

None

 the selected file is not readble because :  [WinError 183] Cannot
 create a file when that file already exists: './search_result'
 'NoneType' object is not iterable

None is the result of: print(searchResultFoder).

The code is:

if not(os.path.exists("./search_result")):                                      
                    today = time.strftime("%Y%m%d__%H-%M")
                    jsonFileName = "{}_searchResult.json".format(today)
                    fpJ = os.path.join(os.mkdir("./search_result"),jsonFileName)
                    print(fpJ)
with open(fpJ,"a") as jsf:
                    jsf.write(jsondata)
                    print("finish writing")
Py Dev
  • 45
  • 8
  • `mkdir` doesn't return any value, so you can't print it, so you're assigning `None` to `searchResultFoder`. – Jean Rostan Apr 09 '18 at 20:08
  • @jean Rostan i know that so how to fix this ?? – Py Dev Apr 09 '18 at 20:11
  • Please [edit] your question and fix the indentation of your sample code. – martineau Apr 09 '18 at 20:14
  • 1
    I don't understand what you're trying to do. You create a new folder with a constant name, but then use `searchResultFoder` instead of that name. Then you seem to expect that your new folder will have files in it ... *while you try to create it a second time*. We can't **fix** code that seems to disagree with itself. – Prune Apr 09 '18 at 20:15
  • @Prune when you say **while you try to create it a second time** where this happen in my code because what i want is .....1) check if folder exist if not create it 2) write the JSON FILE inside this created folder. – Py Dev Apr 09 '18 at 20:31
  • Then use the folder you just created. Your code calls `mkdir` a second time, when you *know* it already exists. That's guaranteed to raise an exception. You already *have* the folder name; use it. – Prune Apr 09 '18 at 20:33
  • i delete the var searchResultFoder and try to create the folder inside **OS.PATH.JOIN** BUT THE SYSTEM still display this error : **expected str, bytes or os.PathLike object, not NoneType 'NoneType' object is not iterable** – Py Dev Apr 09 '18 at 20:56

1 Answers1

0

Issues with code :

  • Case Directory Doesn't exist : os.mkdir("./search_result") in fpJ = os.path.join(os.mkdir("./search_result"),jsonFileName) returns nothing you have assumed it will return you the path of the created folder. which is incorrect.

  • Case Directory exists : In case when condition if not(os.path.exists("./search_result")):
    is false json file name will be undefined and throw and exception

. Full working example of code which is doing the following. 1) check if folder exist if not create it 2) write the JSON FILE inside this created folder.

import json
import os
import time

jsondata = json.dumps({"somedata":"Something"})
folderToCreate = "search_result"
today = time.strftime("%Y%m%d__%H-%M")
jsonFileName = "{}_searchResult.json".format(today)

if not(os.path.exists(os.getcwd()+os.sep+folderToCreate)):
                    os.mkdir("./search_result")

fpJ = os.path.join(os.getcwd()+os.sep+folderToCreate,jsonFileName)
print(fpJ)

with open(fpJ,"a") as jsf:
                    jsf.write(jsondata)
                    print("finish writing")

Hope this helps

toheedNiaz
  • 1,435
  • 1
  • 10
  • 14
  • thank you this solve the problem... i have another problem can you help me with ? the created file includes **multiple lists with 1 dictionary** ... where what i want is to have **1 list with multiple dictionaries itmes** – Py Dev Apr 09 '18 at 21:43
  • sure i can, can you share some example of the dictionary and the lists ? I will suggest you to ask a separate question for this. It will help others with the similar queries. – toheedNiaz Apr 09 '18 at 21:48
  • Accept the answer if it fixes the stated problem – toheedNiaz Apr 09 '18 at 21:50
  • this is the other question but till now no one was able to fix the issue [https://stackoverflow.com/questions/49708847/how-to-create-a-list-of-dictionaries-using-python?noredirect=1#comment86433963_49708847] – Py Dev Apr 09 '18 at 21:52