New to Python 3.4+ Scripting and writing JSON text files and saving them -
i'm trying write script in python can manipulate json ".txt" files within folder , saving new json ".txt" file within same folder. know how manipulate file, i'd need manually type in actual path. there way type in folder name , name of text file manipulate instead of typing full path?
because right now, i'm having do
import json simple_path = input('please input path directory: ") >>> 'c:\user\path1\path2\simple.txt' open (simple_path,'r+') f: simple_data = json.load(f) somefunction() f.seek(0) out_file = open('c://user//path1//path2//simple_edit.txt','w') json.dump(simple_data, out_file)
each time want manipulate specific json text files, i'd have change whole path, , i'd have edit "out_file" manually every single time, can make more flexible?
since i'll placing script main folder sub-folders , json files. it'd easier me manipulate sub-folder json text files without having put full path.
yes can os module
import os folder = input('please enter folder:') file = input('enter filename:') print('c'+os.path.join(os.sep, folder, file))
os.path.join
constructs path file joining strings supplied , adding correct file separator (/
in *nix systems , \
on windows)
while os.sep
returns string of root directory /
in *nix systems , \
in windows