Questions tagged [os.walk]

`os.walk()` is a Python function which serves to walk a directory tree.

With os.walk() you can walk a directory tree and get, for each of the subdirectories, a list of file names.

A simple example goes like

import os
for root, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        print os.join(root, filename)
726 questions
0
votes
1 answer

Os.walk wont work with directory acquired through ' match.group(0).encode('string-escape') '

I'm using os.walk to search for files in specific directories. This is testcode that wont do what it should: import os, re cwd = os.getcwd() directory= 'Box II' dirpattern = re.compile(r'^.*?\\'+directory+'.*?', re.M) for root, dirs, files in…
Baf
  • 2,041
  • 3
  • 16
  • 10
0
votes
1 answer

os.walk raises "unknown exception" in Eclipse/PyDev when path is a windows mapped network drive

I am trying to write a module that searches for different files on mapped drives in a Windows environment. This works perfectly in PythonWin and Idle but raises an exception "Unknown exception when splitting input. Press any key to quit" when run in…
0
votes
2 answers

Why does this recursive copy function copy all files into every directory above the correct one?

I write a function to copy files from directory A to directory B recursive. The code is like this: import os import shutil import sys from os.path import join, exists def copy_file(src, dest): for path, dirs, files in os.walk(src,…
linuxlsx
  • 138
  • 10
-1
votes
1 answer

Python: Create subdirectories at the end of the tree

Please see my directory tree below. I need to add an Aug 2022 folder at the end of the tree for each of Subfolder1, 2, and 3. But these subfolder1, 2, 3 have different levels, for example: The Aug 2022 folder should be nested under…
Alan H
  • 13
  • 4
-1
votes
1 answer

Function that takes folder name as argument (python)

I am trying to write a function that deleting all directories and files except the one (folder_to_exclude) def cleanDir(self,dirpath, folder_to_exclude, handler): for root, dirs, files in walk(dirpath, topdown=True): for file_ in…
Dorga
  • 17
  • 4
-1
votes
2 answers

Python os.walk looping folders in a pattern to rename

Hi I'm trying to learn the os.walk module Thank you in advance Aim: rename all folders in 'student folder' To read 1, 2 ,3 ,4 Problem: My code only renames the first folder to 1 successfully others remain the same. for root, sub, files in…
Ghost786
  • 13
  • 3
-1
votes
1 answer

OS.walk prints my files in a random order, how can i sort the values?

I am trying to export six images from a folder the six images are called 1.png, 2.png etc but when I call os.walk and ask to print the files they come out in a random order: /Users/claudiabergeron/Documents/Python/Python_VideoGame_3.10/bin/python…
-1
votes
1 answer

Find JSON files in a directory and open them

My goal is to search for specific JSON files in a directory and to read them so that I can export the content of them as an Excel file. DIRECTORY LISTING: \Linkedin\linkedin_hb_ma\2021-260\1eb95ebb-d87d-XX1-XXX-XXX1cX Details: (linkedin hb_ma): the…
aymane
  • 3
  • 3
-1
votes
1 answer

print/list only 5 entries from OS.Walk in python

My Goal - To list only 5 entries when using OS walk. So far I have only been able to get a list of everything that I find using OS.Walk or only list one entry. (By using the return function) My code: import os import subprocess def playmusic(name): …
user11322796
-1
votes
2 answers

os.walk() does not print out file and folder names

import os mypath = '/Users/ken/Desktop/myFolder/' for folderName, subfolders, filenames in os.walk(mypath): print('The current folder is ' + folderName) for subfolder in subfolders: print('SUBFOLDER OF ' + folderName + ': ' +…
Ken
  • 49
  • 5
-1
votes
1 answer

if file don't exist using os.walk

Is there an easy way to check that a file exists in the entire folder structure without having to cycle? (the use of os.walk is necessary) "if file exist in in my_dir and its sub directories" for root, dirs, files in os.walk(my_dir): # do…
anas
  • 9
  • 4
-1
votes
2 answers

Error: file doesn't exist and no such file or directory python

I have files (data1.txt,data2.txt,....) that I want to iterate, so I did that: path='mypath' # not the same python project directory for root, dirs, files in os.walk(path): for file in files: print(file) f = open(file) despite the…
MAAHE
  • 169
  • 6
-1
votes
1 answer

os.walk() is not throwing any error nor the result in python

I am trying to print the file names which are in a predefined paths where the paths are stored in paths.txt. But when I execute the below code, I'm not getting any error nor the files names printed. import os with open('D:\paths.txt', 'r') as file: …
Raghavendra
  • 521
  • 5
  • 11
-1
votes
1 answer

how to traverse path and find files and directories using python?

i have a python code that traverse a given path and list all files and directories but the problem is that when a file exist inside a subdirectory the system crash and display FileNotFoundError that the file does not exist in the given path and…
Dev Dj
  • 169
  • 2
  • 14
-1
votes
1 answer

how to copy files and directory from source to destination using python

i want a python script that do these three tasks: check if the path contains word file copied to specific destination check if the path contains pdf files copied to specific destination check if the path contains directory and copy the hole…
Dev Dj
  • 169
  • 2
  • 14