creating a folder and checking for its existence using python
Create a directory and then look to see if it has been created
create_folder("11", "course files", "GQM-test-20160623c") check_for_folder("11", "course files", "GQM-test-20160623c")
These calls use the functions defined below:
def create_folder(course_id, parent_folder_path, folder_name):
url = baseUrl + '%s/folders' %(course_id)
print url
payload = {'parent_folder_path': parent_folder_path, 'name': folder_name}
print payload
write_to_log(course_id + "/" + parent_folder_path + "/" + folder_name)
r = requests.post(url, headers = header, data = payload)
write_to_log(r.text)
def check_for_folder(course_id, parent_folder_path, folder_name):
url = baseUrl + '%s/folders' %(course_id)
write_to_log("folders of " + course_id)
payload = {}
r = requests.get(url, headers = header, data = payload)
write_to_log(r.text)
folders_response=r.json()
full_name_to_match='%s/%s' % (parent_folder_path, folder_name)
print "full_name" + "\t" + "id" + "\t" + "matching: "+ full_name_to_match
for f in folders_response:
if (f["full_name"] == full_name_to_match):
print f["full_name"] + "\t" + str(f["id"]) + "\ttrue"
else:
print f["full_name"] + "\t" + str(f["id"])
Full source code at: create-directory-check-it-exits.py Download create-directory-check-it-exits.py
Before:
./create-directory-check-it-exits.py
# will output (assuming that the folder does not already exist):
./create-directory-check-it-exits.py 'Wed Jun 22 19:59:42 2016' https://kth.instructure.com/api/v1/courses/11/folders {'parent_folder_path': 'course files', 'name': 'GQM-test-20160623c'} '11/course files/GQM-test-20160623c' u'{"id":344, ... "for_submissions":false}' 'folders of 11' u'[{"id":224,"name":"course files",... ]' full_name id matching: course files/GQM-test-20160623c
course files 224 course files/GQM-test-20160623c 343 true course files/GQM-test-20160623c 2 344 course files/II2202-Fall-2014 314 course files/II2202-Fall-2015 311 course files/II2202-Fall-2016 283 course files/Test-folder 321 'Wed Jun 22 19:59:42 2016' '\n--DONE--\n\n'
After:
Unless you specify otherwise, if you run the same program again you will get a new directory with the name: "GQM-test-20160623c 2"
Running it yet again there will a version "3", etc.