Re: [Tutor] creating .json files

2017-04-14 Thread Rafael Knuth
> You need to open the file to create it:

Ok, got you. Thanks, Alan!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating .json files

2017-04-13 Thread Alan Gauld via Tutor
On 13/04/17 17:32, Rafael Knuth wrote:
> Is there a way to split these two into separate steps:
> a) creating a .json file
> b) manipulating it (a, r, w ...)

Of course.

> What if I just wanted to create a .json file and do nothing with it?
> 
> import json
> file_name = "my_numbers.json"
> 
> The above does not do the job. What am I getting wrong here? Thanks.

The above creates a variable called file_name that stores a string.
It has nothing to do with any files.

You need to open the file to create it:

with open(file_name,'w') as json_file: pass

Will open a new file and immediately close it again.

You could do the same explicitly with

json_file = open(file_name,'w')
json_file.close()

Remember that variable names are just labels for your benefit.
The fact that you call it file_name means nothing to Python,
you might as well call it xcdseqplrtyg123 so far as Python is concerned,
its just a lablel. The name is only meaningful to
you (and possibly to other human readers).

Similarly, although your string looks like a file name to
a human reader, to Python it's just a string of characters.
Python cannot draw any meaning from that.

Finally, the code above creates a new file called my_numbers.json
But it is an empty file and is NOT a json file, despite the name.
It only becomes a json file once you add some json data to it.
open() creates text files, it has no understanding of what the
data you write to those files means.

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating .json files

2017-04-13 Thread leam hall
On Thu, Apr 13, 2017 at 12:32 PM, Rafael Knuth 
wrote:

> Is there a way to split these two into separate steps:
> a) creating a .json file
> b) manipulating it (a, r, w ...)
>
> Example:
>
> "import json
> number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
> file_name = "my_numbers.json"
> with open(file_name, "w") as a:
> json.dump(number_list, a)
>
> What if I just wanted to create a .json file and do nothing with it?
>
> import json
> file_name = "my_numbers.json"
>
> The above does not do the job. What am I getting wrong here? Thanks.
>
> If you want to have a file after the program runs you need to "open" it.
And close it. Otherwise you reference a file but leave no trace.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor