file-io

Regex pattern returns null for variable as pattern

Regex pattern returns null for variable as pattern Question: I am reading regex pattern from a .txt file and passing it as a variable and using it to search in a very big text file. However, variable passing in regex search didnt work. My code snippet is with open(r"C:Desktoplist_pattern.txt", "r") as file1: for pattern in …

Total answers: 2

Reading multiple txt files from multiple folders

Reading multiple txt files from multiple folders Question: I have 20 folders, each containing 50 txt files, I need to read all of them in order to compare the word counts of each folder. I know how to read multiple files in one folder, but it is slow, is there a more efficient way instead …

Total answers: 2

How to open a file in binary mode in google storage bucket from cloud function?

How to open a file in binary mode in google storage bucket from cloud function? Question: In my cloud function, I need to get a file in cloud storage and send the file to an API through HTTP POST request. I tried the following code: storage_client = storage.Client() bucket = storage_client.bucket(BUCKET_NAME) source_blob_name = "/compressed_data/file_to_send.7z" blob …

Total answers: 2

Writing tensor to a file in a visually readable manner

Writing tensor to a file in a visually readable manner Question: In pytorch, I want to write a tensor to a file and visually read the file contents. For example, consider T = torch.tensor([3,4,5,6]). I want to write the tensor T to a file, say file_T.txt, and want to visually read the contents of the …

Total answers: 2

How do I get the child folder name of the path besides these methods?

How do I get the child folder name of the path besides these methods? Question: Of the given path like “level1/level2/level3/”, I’d like pass it through some operation and get the result like “level3/”. So I made two trials like these: TRIAL 1: After finding parent property within the Path object, I looked for something …

Total answers: 2

How to fix 'RuntimeError: input(): lost sys.stdin' error in python 3.7

How to fix 'RuntimeError: input(): lost sys.stdin' error in python 3.7 Question: I am practicing some codes and seemingly out of nowhere i have got this error when I ran a very usual piece of code. The problem i am solving takes input, calculates something and gives an output. I was running it on an …

Total answers: 8

How do I iterate over files in a directory including sub directories, using Python?

How do I iterate over files in a directory including sub directories, using Python? Question: I try writing a script that counts the lines of code in a project. The problem I have is it doesn’t find all files. The script looks like this: import os root = r"C:Usersusernamedataprojectstest" allLines = [] for path, subdirs, …

Total answers: 1

TypeError: 'newline' is an invalid keyword argument for this function, when trying to open() file

TypeError: 'newline' is an invalid keyword argument for this function, when trying to open() file Question: I wrote the following code which extracts the info. of a file and orders it alphabetically based on its second column objects: import csv import operator import sys def re_sort(in_file=’books.csv’, out_file=’books_sort.csv’): data = csv.reader(open(‘books.csv’, newline=”), delimiter=’,’) header = next(data) …

Total answers: 2