string - Python won't rewrite file with first line removed using readlines() and for loop -
string - Python won't rewrite file with first line removed using readlines() and for loop -
i' writing web scraping script in python pulls 'id' first line of text file , appends partial url; downloads image said url. works fine, when run using 3rd function (which contains actual code downloading - works fine) doesn't update text file deleting first line.
i'm sure there missing here, , help appreciated.
def delete_used_id(): # open , save ids id_file_r = open('c:/users/my.name/documents/ids.txt', 'r') lines = id_file_r.readlines() first_line_unf = str(lines[0])[3:] first_line = first_line_unf.strip() id_file_r.close() # reopen in write mode id_file_w = open('c:/users/my.name/documents/ids.txt','w') unused_id in lines: if unused_id != first_line: id_file_w.write(unused_id) id_file_w.close() print("deleting used id: " + first_line) def get_id_from_file(): id_file = open('c:/users/my.name/documents/ids.txt', 'r') # piece first 3 chars off of string farmer_id_unf = id_file.readline()[3:] farmer_id = farmer_id_unf.strip() print("getting farmer id: " + farmer_id) homecoming farmer_id
thanks jonrsharpe , nerdwaller:
answer:
def delete_used_id(): # open , save ids id_file_r = open('c:/users/my.name/documents/ids.txt', 'r') lines = id_file_r.readlines() first_line_unf = lines[0][3:] first_line = first_line_unf.strip() id_file_r.close() # reopen in write mode id_file_w = open('c:/users/my.name/documents/ids.txt','w') line in lines[1:]: id_file_w.write(line) id_file_w.close() print("deleting used id: " + first_line)
python string file-io web-scraping
Comments
Post a Comment