file-handling

Clean way to open/close an optional file in python?

Clean way to open/close an optional file in python? Question: My code: fh = None if args.optional_input_file_path is not None: fh = open(args.optional_input_file_path) my_function(foo, bar, fh) if args.optional_input_file_path is not None: fh.close() I don’t like how I need to write if args.optional_input_file is not None: twice. Moving the conditional logic inside my_function also isn’t ideal …

Total answers: 3

read spreadsheet online and dump data in folders using python?

read spreadsheet online and dump data in folders using python? Question: this code which read spreadsheet online import pandas as pd sheet_id = "example6565465dchdckjdISHEETID" df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv") print(df) my folder structure to read csv file code is that import csv import os root = r’path’ with open(r"csv_path", ‘r’) as csv_file: df = csv.DictReader(csv_file) for row …

Total answers: 1

It's showing n when printing the list

It's showing n when printing the list Question: The output: {‘name’: ‘Peter’, ‘surname’: ‘ Abdilla’, ‘DOB’: ‘ 22/02/1986’, ‘mobileNo’: ‘79811526’, ‘locality’: ‘ Zabbarn’} {‘name’: ‘John’, ‘surname’: ‘ Borg’, ‘DOB’: ‘ 12/04/1982’, ‘mobileNo’: ‘99887654’, ‘locality’: ‘ Paolan’} The expected output is supposed to be: {‘name’: ‘Peter’, ‘surname’: ‘ Abdilla’, ‘DOB’: ‘ 22/02/1986’, ‘mobileNo’: ‘79811526’, ‘locality’: ‘ …

Total answers: 1

creating a leaderboard for noughts and crosses game

creating a leaderboard for noughts and crosses game Question: {"Bob": 50,"Jane": 2,"Sarah": 5, "Amelia": 20} I have a txt file like this and I wrote a code to be able to get different players name and score that will be saved to the dictionary from highest to lowest.but when ever a new player saves their …

Total answers: 1

Why isn't the JSON file being outputted to the folder being created?

Why isn't the JSON file being outputted to the folder being created? Question: I am trying to implement openpose on google colab on a set of videos. My problem is I want to specifically output the JSON files to folders named according to the name of the video file. For example, the JSON files of …

Total answers: 1

sort text file and and sort the number of listing

sort text file and and sort the number of listing Question: I want to sort a text file every time a new input was added, but I want the numbers for each name also get sorted. but when I used split() I get errors : I/O operation on closed file. am I putting it in …

Total answers: 2