listdir

Python tkinter cut down path but open as full directory

旧城冷巷雨未停 提交于 2021-01-29 11:52:29
问题 Here is my full Python code: from tkinter import * import glob import os from PIL import Image, ImageTk, ImageGrab import tkinter as tk import pyautogui import datetime #date & time now = datetime.datetime.now() root = tk.Tk() root.title("SIGN OFF") root.minsize(840, 800) # Add a grid mainframe = tk.Frame(root) mainframe.columnconfigure(0, weight=1) mainframe.rowconfigure(0, weight=1) mainframe.pack(pady=100, padx=100) # Create a Tkinter variable tkvar = tk.StringVar(root) # Directory

How to find files in multilevel subdirectories

南楼画角 提交于 2020-01-30 11:21:05
问题 Suppose I have a directory that contains multiple subdirectories: one_meter = r"C:\Projects\NED_1m" Within the directory one_meter I want to find all of the files that end with '.xml' and contain the string "_meta". My problem is that some of the subdirectories have that file one level donw, while others have it 2 levels down EX: one_meter > USGS_NED_one_meter_x19y329_LA_Jean_Lafitte_2013_IMG_2015 > USGS_NED_one_meter_x19y329_LA_Jean_Lafitte_2013_IMG_2015_meta.xml one_meter > NY_Long_Island>

measure progress (time left) while os.listdir is generating a list (Python)

青春壹個敷衍的年華 提交于 2020-01-02 14:56:53
问题 In Python 2.7, I am using os.listdir to generate a list of files in a folder. There are lots of files and my connection to the folder is slow so it can take up to 30 seconds to complete. Here is an example: import os import time start_time = time.time() dir_path = r'C:\Users\my_name\Documents\data_directory' #example path file_list = os.listdir(dir_path) print 'it took', time.time() - start_time, 'seconds' This is for a Tkinter GUI I'm working on and I would like make a status bar showing how

os.path.isfile() doesn't work. Why?

别等时光非礼了梦想. 提交于 2019-12-17 17:08:16
问题 I'm trying to do this: import os [x for x in os.listdir('.') if os.path.isfile(x)] [x for x in os.listdir('dirname') if os.path.isfile(x)] [x for x in os.listdir(os.path.abspath('dirname')) if os.path.isfile(os.path.abspath(x))] The first line works: [x for x in os.listdir('.') if os.path.isfile(x)] But the next two: [x for x in os.listdir('dirname') if os.path.isfile(x)] and [x for x in os.listdir(os.path.abspath('dirname')) if os.path.isfile(os.path.abspath(x))] just output [] Why? 回答1:

Iterate all subfolders and OCR images in Python

我怕爱的太早我们不能终老 提交于 2019-12-11 14:18:20
问题 I have a folder which have multiple subfolders and images, and I want use Baidu OCR to extract texts in images files in for each subfolders and write to one excel (need split contents) file for each subfolder named by subfolders name: folder \ sub1\file0.jpg \ sub1\file1.jpg \ sub1\file2.png . . . \ sub2\xxx.png \ sub2\yyy.jpg \ sub2\zzz.png . . . Expected results: folder \ sub1\file0.jpg \ sub1\file1.jpg \ sub1\file2.png \ sub1\sub1.xlsx . . . \ sub2\xxx.png \ sub2\yyy.jpg \ sub2\zzz.png \

How to select files with numbered extensions from a folder?

允我心安 提交于 2019-12-11 11:28:36
问题 I am trying to build my own dataset for a project. Therefore I need to select files that have been exported from another program and come with numbered extensions: exported_file_1_aaa.001 exported_file_2_aaa.002 exported_file_3_aaa.003 ... exported_file_5_zzz.925 ...and so on. I know how to select files with a specific extension e.g. '.txt' from a folder and append it to a list or dict. Is there any way to solve this with '.nnn' ext = '.nnn' all_files = [i for i in os.listdir(dir) if os.path

Select files in directory and move them based on text list of filenames

拈花ヽ惹草 提交于 2019-12-08 11:08:34
问题 So I have a folder of a few thousand pdf files in /path, and I have a list of hundreds of names called names.csv (only one column, it could just as easily be .txt). I'm trying to select (and ideally, move) the pdfs, where any name from names.csv is found in any filename. From my research so far, it seems like listdir and regex is one approach to at least get a list of the files I want: import os, sys import re for files in os.listdir('path'): with open('names.csv') as names: for name in names

Python: how to get the first file in directory?

こ雲淡風輕ζ 提交于 2019-12-07 21:05:58
问题 So I want to grab the first file under a directory in Python. I know I can do like this: first_file = [join(path, f) for f in os.listdir(path) if isfile(join(path, f))][0] But it's slow. Is there any better solution? Thanks! 回答1: You can use next(): first_file = next(join(path, f) for f in os.listdir(path) if isfile(join(path, f))) Note that if there are no files in the directory it would throw StopIteration exception. Either handle it, or provide a default value : first_file = next((join

Python: how to get the first file in directory?

本秂侑毒 提交于 2019-12-06 13:54:55
So I want to grab the first file under a directory in Python. I know I can do like this: first_file = [join(path, f) for f in os.listdir(path) if isfile(join(path, f))][0] But it's slow. Is there any better solution? Thanks! You can use next() : first_file = next(join(path, f) for f in os.listdir(path) if isfile(join(path, f))) Note that if there are no files in the directory it would throw StopIteration exception. Either handle it, or provide a default value : first_file = next((join(path, f) for f in os.listdir(path) if isfile(join(path, f))), "default value here") 来源: https://stackoverflow

Recursive SFTP listdir in Python?

十年热恋 提交于 2019-12-01 13:53:22
I need to recursively list the content of a directory that contains a lot of subdirectories (more than 16,000). I am currently using Paramiko's SFTP client , which doesn't offer any recursive listdir functionality. So I have to first run listdir on the parent folder, and then another listdir for each of the (many, many) subdirectories. It takes too long to run. Is there any way to run the recursive listdir in a single SFTP call? I'm not limited to the Paramiko package, it's just the package that we're currently using. I cannot use Paramiko's ssh.exec_command('ls -R <path>') due to local