glob

Open multiple files with the same extension in a folder

Open multiple files with the same extension in a folder Question: I want to open all files with the same extension (.dat) in the same directory. This is my current code: path = Path(".") # current directory extension = ".dat" file_with_extension = next(path.glob(f"*{extension}")) if file_with_extension: with open(file_with_extension, encoding=’utf-8′) as infile, open(‘462888.dat’, ‘w’, encoding=’utf-8′) as outfile: …

Total answers: 1

Iterate through all sheets of all workbooks in a directory

Iterate through all sheets of all workbooks in a directory Question: I am trying to combine all spreadsheets from all workbooks in a directory into a single df. I’ve tried with glob and with os.scandir but either way I keep only getting the first sheet of all workbooks. First attempt: import pandas as pd import …

Total answers: 1

Batch Rename files with different Prefixes but same file type using python

Batch Rename files with different Prefixes but same file type using python Question: I am trying to rename my files incrementally with a counter starting at 1, which process them in a way depending on their prefix and same file extension. The directory has the following files examples: BS – foo.fxp BS – bar.fxp BS …

Total answers: 2

return found path in glob

return found path in glob Question: If I have a glob(‘path/to/my/**/*.json’, recursive = True), function returns something like: path/to/my/subfolder1/subfolder2/file1.json path/to/my/subfolder1/subfolder2/file2.json path/to/my/subfolder1/subfolder2/file3.json path/to/my/subfolder1/file4.json path/to/my/file5.json … I’d like to get only part that starts after ** in the glob, so subfolder1/subfolder2/file1.json subfolder1/subfolder2/file2.json subfolder1/subfolder2/file3.json subfolder1/file4.json file5.json … What is the best way to do it? Does glob support …

Total answers: 1

Is there a way to read multiple plain text files into a dataframe?

Is there a way to read multiple plain text files into a dataframe? Question: I have multiple plain text files that need to be saved in each row in a data frame. I want to make the data frame consist of two columns: the filenames and texts. The code below does not spit error message, …

Total answers: 2

Using a for loop to rename processed .wav file with pydub AudioSegment function

Using a for loop to rename processed .wav file with pydub AudioSegment function Question: I am trying to rename multiple files in a loop using the AudioSegment function. Below is the function for a single file (which works perfectly from pydub import AudioSegment audio = AudioSegment.from_wav("./OM1/BMM.wav") new_audio = audio[1000:len(audio)-1000] new_audio.export(‘newSong.wav’, format="wav") Now I want to …

Total answers: 2

Get directories only with glob pattern using pathlib

Get directories only with glob pattern using pathlib Question: I want to use pathlib.glob() to find directories with a specific name pattern (*data) in the current working dir. I don’t want to explicitly check via .isdir() or something else. Input data This is the relevant listing with three folders as the expected result and one …

Total answers: 2

Pandas Read CSV Error When Reading Multiple Files

Pandas Read CSV Error When Reading Multiple Files Question: I have multiple csv files, named as 2C-BEB-29-2009-01-18.csv,2C-BEB-29-2010-02-18.csv,2C-BEB-29-2010-03-28.csv, 2C-ISI-12-2010-01-01.csv, and so on. 2C- Part is default in all csv files. BEB means name of the recording device 29 stands for the user ID 2009-01-18 stands for the date of the recording. I have around 150 different …

Total answers: 1

Parse unique filenames better in python/Snakemake

Parse unique filenames better in python/Snakemake Question: I have a series of samples that will be names unique header. For instance the sample could be labeled (SC892138_CTGAAGCT-ACTCTGAG or SC892138_unisample)_L001_001.star_rg_added.sorted.dmark.bam. Or(SC892155_CTGAAGCT-ACTCTGAG or SC892155_unisample)_L001_001.star_rg_added.sorted.dmark.bam. Or some other number after SC. I can parse out SC###### value for each sample, but I want to get to this:SC892155_CTGAAGCT-ACTCTGAG. How …

Total answers: 1