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
-3
votes
2 answers

python for loop in os.walk()

i write this code to get the count of files inside my directory but i need it to count the output lines when it run ... import os for dir,subdir,files in os.walk(r"C:\Users\adam\Desktop\test"): i = 0 i = i + 1 print(str(i) + ": files…
evilcode1
  • 111
  • 2
  • 10
-3
votes
3 answers

Python class error TypeError: __init__() missing 1 required positional argument: 'self'

I have a python class as following. class copyingfiles(): @staticmethod def __init__(self, x=[], y=[], z=None, i=None): self.x = x self.y = y self. z = z self.i= i @staticmethod def mover(self): …
ARJ
  • 2,021
  • 4
  • 27
  • 52
-3
votes
2 answers

Python: create a dictionary with elements consist of file, path and the content

The issue I'm facing is that I have to find each file that ends in ".txt" and read it's content to return a dictionary. Returned dictionary should looks like this: dic = { 'Folder\\\fileName.txt' : "This is content" } So far I have: directory =…
John Doe
  • 53
  • 4
  • 14
-3
votes
2 answers

Output formatting of a list

Have a piece of code in python : print (sorted([(r,len(f)) for r,d,f in os.walk(directory_path)],key=lambda x : x[1],reverse = True)) which gives output like : [('/etc/ssl/certs', 595), ('/etc/alternatives', 109), ('/etc/', 73), ('/etc/init.d',…
-3
votes
1 answer

Python: Writing a script that print filenames of zerolength files and a script that counts all images in a web page

I am currently learning Python, and could really use help from experienced coders with help to getting started on solving this assignment: Using os.walk, write a script that will print the filenames of zero length files. It should also print the…
-3
votes
2 answers

Does os.walk() use recursion or iteration?

Okay I have seen some people say os.walk() in Python uses recursion and some say it uses iteration. What is the correct answer?
sabzdarsabz
  • 343
  • 5
  • 15
1 2 3
48
49