os.walk

IOError: [Errno 2] No such file or directory – os.walk

房东的猫 提交于 2020-01-30 10:58:36
问题 I'm trying to run the following script which simply reads and image and saves it again: from PIL import Image import os rootdir = '/home/user/Desktop/sample' for subdir, dirs, files in os.walk(rootdir): for file in files: im = Image.open(file) im.save(file) I however get the following error: Traceback (most recent call last): File "test.py", line 10, in <module> im = Image.open(file) File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open fp = builtins.open(filename, "rb")

IOError: [Errno 2] No such file or directory – os.walk

ぐ巨炮叔叔 提交于 2020-01-30 10:54:25
问题 I'm trying to run the following script which simply reads and image and saves it again: from PIL import Image import os rootdir = '/home/user/Desktop/sample' for subdir, dirs, files in os.walk(rootdir): for file in files: im = Image.open(file) im.save(file) I however get the following error: Traceback (most recent call last): File "test.py", line 10, in <module> im = Image.open(file) File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open fp = builtins.open(filename, "rb")

Trying to create a CSV file with a file search using os.path

前提是你 提交于 2020-01-30 02:38:40
问题 I want to open the main folder containing all files (1), search though the files and only grab any .txt file with "mtn" in the title (2), print a the list of txt files (3) then list the txt files in a csv file, including their full path (4). I can do (1) through (3) with my current code, however the CSV file that gets produced only contains the last filename, so I think there is something wrong with the order of my loops mtnpath = r"G:\somepath\" num_files = 0 for root, dirs, files in os.walk

Make empty file for each subfolder using subfolders' name in Python

时光总嘲笑我的痴心妄想 提交于 2020-01-15 08:12:22
问题 If I have a folder structure as follows: folder \ sub1\sub1_1 \ sub1\sub1_2 \ sub1\sub1_3 . . . \ sub2\sub2_1 \ sub2\sub2_2 \ sub2\sub2_3 . . . I want make files for each subfolder use subfolders' name. How can I do it in Python? Thanks. Expected result: folder \ sub1\sub1_1\sub1_1.xlsx \ sub1\sub1_2\sub2_2.xlsx \ sub1\sub1_3\sub3_3.xlsx . . . \ sub2\sub2_1\sub2_1.xlsx \ sub2\sub2_2\sub2_2.xlsx \ sub2\sub2_3\sub2_3.xlsx . . . Here is what I have tried: import os dir_name = "D:/folder" for

python storing path names with forward vs backward slash

╄→гoц情女王★ 提交于 2020-01-13 19:03:09
问题 I have a procedure that os.walk s a directory and its subdirectories to filter pdf files, separating out their names and corresponding pathnames. The issue I am having is that it will scan the topmost directory and print the appropriate filename e.g. G:/Books/Title.Pdf but the second it scans a subfolder e.g G:/Books/Sub Folder/Title.pdf it will print the following G:/Books/Sub Folder\\Title.Pdf (which is obviously an invalid path name). It will also add \\ to any subfolders within subfolders

python storing path names with forward vs backward slash

99封情书 提交于 2020-01-13 19:02:10
问题 I have a procedure that os.walk s a directory and its subdirectories to filter pdf files, separating out their names and corresponding pathnames. The issue I am having is that it will scan the topmost directory and print the appropriate filename e.g. G:/Books/Title.Pdf but the second it scans a subfolder e.g G:/Books/Sub Folder/Title.pdf it will print the following G:/Books/Sub Folder\\Title.Pdf (which is obviously an invalid path name). It will also add \\ to any subfolders within subfolders

ioerror errno 13 permission denied: 'C:\\pagefile.sys'

空扰寡人 提交于 2020-01-07 08:37:41
问题 Below is my code, what I am trying to achieve is walking through the OS generating a MD5 hash of every file the code is functional, however, I receive the error in the title "ioerror errno 13 permission denied: 'C:\pagefile.sys'" when I try to run the file from C:\ is there a way i can run this as an admin? Even when I run cmd as an admin that does not work, thank you in advance. import os, hashlib current_dir = os.getcwd() for root,dirs,files in os.walk(current_dir): for f in files: current

Iteratively (or recursively?) filter directory and copy results to new directory

两盒软妹~` 提交于 2020-01-07 05:43:06
问题 I’m new to Python and have been teaching myself with textbooks and StackOverflow. I seem to learn best from real world examples from this site. I’m hoping someone can help me with my current problem. I have a directory (folder) with about 1,000 files. The files have names with prefixes (patterns) such as, aaa_* , bbb_* , ccc_* and so on. I would like to iterate through the directory and copy all the files that have the specified prefix and move them to a new directory with that prefix name.

Use original file name to save image

天涯浪子 提交于 2020-01-06 07:45:17
问题 I want to save image with original file name for example if original file name is hero so the processed image name should be hero_Zero.png . I dont know how to pass file name in def run(dirs, img): Below code is correct and modified. def run(dirs, img, file_): for (x, y) in labels: component = uf.find(labels[(x, y)]) labels[(x, y)] = component if labels[(x, y)]==0: Zero[y][x]=int(255) count=count+1 if count<=43: continue elif count>43: Zeroth = Image.fromarray(Zero) Zeroth.save(os.path.join

How to stop traversing current root and iterate to the next one

☆樱花仙子☆ 提交于 2020-01-05 06:42:32
问题 I'm writing a simple function that walks through a directory tree looking for folders of a certain name. What I'm after is a match's parent path. E.g., for "C:/a/b/c/MATCH" I want "C:/a/b/c". I don't need duplicate parents or paths to subfolder matches, so if there's a "C:/a/b/c/d/e/f/MATCH", I don't need it. So, during my walk, once I have a parent, I want to iterate to the next current root. Below is what I have so far, including a comment where I'm stuck. def FindProjectSubfolders