directory

Rename files in subdirectories with the root directory name in an zipfile

Rename files in subdirectories with the root directory name in an zipfile Question: I have the following directory structure within my zip file: myzip.zip – directory 1 – subdirectory 1 – imageA.jpg – imageB.jpg – directory 2 – subdirectory 2 – imageA.jpg – imageB.jpg And my goal is to rename the .jpg files to main …

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

How to create a Python nested dictionary based on a FTP tree of folders

How to create a Python nested dictionary based on a FTP tree of folders Question: I’m trying to create a dictionary based on a tree of folders located on a FTP server (see image below): The result I’m looking for is : A primary dictionnary that have secondary dictionnaries (as keys) with some lists (as …

Total answers: 1

Launch persistent context from current directory in playwright

Launch persistent context from current directory in playwright Question: I’m using this code I found on this page to save browser context after running the code: user_dir = ‘/tmp/playwright’ with sync_playwright() as p: browser = p.chromium.launch_persistent_context(user_dir, headless=False) page = browser.new_page() But when I try to change the directory from the /tmp/playwright folder, which is created …

Total answers: 2

Reference a directory outside of folder

Reference a directory outside of folder Question: How do I reference a file outside of the file’s folder? I have tried src, but that does not work. f = open(‘src/data/beemovie.txt’,’r’) txt = f.readlines() print(txt) f.close() Asked By: dead inside || Source Answers: Assume your project structure is like below: src └── data ├── beemovie.txt └── …

Total answers: 1

How to copy first n images from a directory to another directory?

How to copy first n images from a directory to another directory? Question: I need to copy the first 1400 images from one directory to another. The code I’ve seen: fnames = [‘{}.app_res_model.tiff’.format{i} for i in range(1400)] for fname in fnames: src = os.path.join(dataset_dir, fname) dst = os.path.join(train_dir_input, fname) shutil.copyfile(src, dst) As my images are …

Total answers: 1

Python 2.7 – Script to rename _TMP folder

Python 2.7 – Script to rename _TMP folder Question: I try to create a script to rename some folder with "_TMP" lasts digits import os basedir = ‘E:Test’ for fn in os.listdir(basedir): if not os.path.isdir(os.path.join(basedir, fn)): continue # Not a directory if ‘_TMP’ not in fn: continue # Invalid format firstname = fn.rpartition(‘_TMP’) os.rename(os.path.join(basedir, fn),os.path.join(basedir, …

Total answers: 1

How to copy a folder into an existing folder using python

How to copy a folder into an existing folder using python Question: I am new to python, and am currently trying to copy a folder ‘foo’ (potentially with some files/folders inside) into another existing folder ‘bar’. After this, there should be a new path created that should look something like this "…bar/foo/*". By copying ‘foo’ …

Total answers: 2

Python: ModuleNotFoundError: No module named 'xyz'

Python: ModuleNotFoundError: No module named 'xyz' Question: I have a folder, called ABC that contains a file called file_function.py, i.e. ABC file_function.py I have a folder in ABC called folder_function that contains a file called main_function.py, i.e. ABC folder_function main_function.py In main_function.py I am calling a function defined in file_function.py called xyz. I am using …

Total answers: 3

Multiple download – CSV file

Multiple download – CSV file Question: I have a script, below, that can download files from a particular row from 1 only CSV file. I have no problem with it, it works well and all files are downloaded into my ‘Python Project’ folder, root. But I would like to add functions here, First, download not …

Total answers: 2