Python File Tree + Size and countering number of files and directoriesDirectory-tree listing in PythonHow do I copy a file in Python?How can I safely create a nested directory?How do I get the number of elements in a list?How to check file size in python?How do I list all files of a directory?Find all files in a directory with extension .txt in PythonHow do you append to a file in Python?How to find if directory exists in Pythonfatal error: Python.h: No such file or directory

Why do people say "I am broke" instead of "I am broken"?

Adding one more column to a table

Are gangsters hired to attack people at a train station classified as a terrorist attack?

How can I show that the speed of light in vacuum is the same in all reference frames?

Why did computer video outputs go from digital to analog, then back to digital?

Is it OK to accept a job opportunity while planning on not taking it?

How could Barty Crouch Jr. have run out of Polyjuice Potion at the end of the Goblet of Fire movie?

Found more old paper shares from broken up companies

How can I calculate the cost of Skyss bus tickets

Character Arcs - What if the character doesn't overcome the big lie, flaws or wounds?

Can an infinite group have a finite number of elements with order k?

What rules turn any attack that hits a given target into a critical hit?

Can't understand how static works exactly

On the history of Haar measure

Has Peter Parker ever eaten bugs?

Correct use of smash with math and root signs

Why is there an extra "t" in Lemmatization?

Can I pay with HKD in Macau or Shenzhen?

How can I deal with someone that wants to kill something that isn't supposed to be killed?

Wiring IKEA light fixture into old fixture

Why can't a country print its own money to spend it only abroad?

What is the best word describing the nature of expiring in a short amount of time, connoting "losing public attention"?

Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?

What does a black-and-white Puerto Rican flag signify?



Python File Tree + Size and countering number of files and directories


Directory-tree listing in PythonHow do I copy a file in Python?How can I safely create a nested directory?How do I get the number of elements in a list?How to check file size in python?How do I list all files of a directory?Find all files in a directory with extension .txt in PythonHow do you append to a file in Python?How to find if directory exists in Pythonfatal error: Python.h: No such file or directory






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-3















I currently have this:



import os

def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print("F"+'/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print("D"+''.format(subindent, f))
list_files('.')


The problem is now that I need to do this:
Make an executable Python script which prints a file tree as follows:



D or F [Filename/Directoryname] [Filesize/countfilesindirectory]



I only need to add this one, but I do not know how:
the file size or number of files in the directory



What I do want to improve it that you also can choose the location by yourself by before executing asking for input with



location = input("Location: ")


This location should get scanned for those directories recursively and show them in a tree.
If someone could help me that would be very useful!



Thanks in advance



~Blackd00r










share|improve this question
























  • Where is the question? What did you try? What are your progress?

    – Guybrush
    Mar 26 at 12:54











  • import os def tree_printer(root): for root, dirs, files in os.walk(root): for d in dirs: print ("D"+os.path.join(root, d)) for f in files: print ("F"+os.path.join(root, f)) tree_printer('.')

    – Blackd00r
    Mar 26 at 13:29

















-3















I currently have this:



import os

def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print("F"+'/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print("D"+''.format(subindent, f))
list_files('.')


The problem is now that I need to do this:
Make an executable Python script which prints a file tree as follows:



D or F [Filename/Directoryname] [Filesize/countfilesindirectory]



I only need to add this one, but I do not know how:
the file size or number of files in the directory



What I do want to improve it that you also can choose the location by yourself by before executing asking for input with



location = input("Location: ")


This location should get scanned for those directories recursively and show them in a tree.
If someone could help me that would be very useful!



Thanks in advance



~Blackd00r










share|improve this question
























  • Where is the question? What did you try? What are your progress?

    – Guybrush
    Mar 26 at 12:54











  • import os def tree_printer(root): for root, dirs, files in os.walk(root): for d in dirs: print ("D"+os.path.join(root, d)) for f in files: print ("F"+os.path.join(root, f)) tree_printer('.')

    – Blackd00r
    Mar 26 at 13:29













-3












-3








-3








I currently have this:



import os

def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print("F"+'/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print("D"+''.format(subindent, f))
list_files('.')


The problem is now that I need to do this:
Make an executable Python script which prints a file tree as follows:



D or F [Filename/Directoryname] [Filesize/countfilesindirectory]



I only need to add this one, but I do not know how:
the file size or number of files in the directory



What I do want to improve it that you also can choose the location by yourself by before executing asking for input with



location = input("Location: ")


This location should get scanned for those directories recursively and show them in a tree.
If someone could help me that would be very useful!



Thanks in advance



~Blackd00r










share|improve this question
















I currently have this:



import os

def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print("F"+'/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print("D"+''.format(subindent, f))
list_files('.')


The problem is now that I need to do this:
Make an executable Python script which prints a file tree as follows:



D or F [Filename/Directoryname] [Filesize/countfilesindirectory]



I only need to add this one, but I do not know how:
the file size or number of files in the directory



What I do want to improve it that you also can choose the location by yourself by before executing asking for input with



location = input("Location: ")


This location should get scanned for those directories recursively and show them in a tree.
If someone could help me that would be very useful!



Thanks in advance



~Blackd00r







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 14:06







Blackd00r

















asked Mar 26 at 12:41









Blackd00rBlackd00r

14 bronze badges




14 bronze badges












  • Where is the question? What did you try? What are your progress?

    – Guybrush
    Mar 26 at 12:54











  • import os def tree_printer(root): for root, dirs, files in os.walk(root): for d in dirs: print ("D"+os.path.join(root, d)) for f in files: print ("F"+os.path.join(root, f)) tree_printer('.')

    – Blackd00r
    Mar 26 at 13:29

















  • Where is the question? What did you try? What are your progress?

    – Guybrush
    Mar 26 at 12:54











  • import os def tree_printer(root): for root, dirs, files in os.walk(root): for d in dirs: print ("D"+os.path.join(root, d)) for f in files: print ("F"+os.path.join(root, f)) tree_printer('.')

    – Blackd00r
    Mar 26 at 13:29
















Where is the question? What did you try? What are your progress?

– Guybrush
Mar 26 at 12:54





Where is the question? What did you try? What are your progress?

– Guybrush
Mar 26 at 12:54













import os def tree_printer(root): for root, dirs, files in os.walk(root): for d in dirs: print ("D"+os.path.join(root, d)) for f in files: print ("F"+os.path.join(root, f)) tree_printer('.')

– Blackd00r
Mar 26 at 13:29





import os def tree_printer(root): for root, dirs, files in os.walk(root): for d in dirs: print ("D"+os.path.join(root, d)) for f in files: print ("F"+os.path.join(root, f)) tree_printer('.')

– Blackd00r
Mar 26 at 13:29












1 Answer
1






active

oldest

votes


















0














This solution to the problem is:



I have added a counter for the file and the directories which start both at 0. In the forloop we add the length of the dir so the total can be calculated.



print(" files in folders".format(filecount, dircount))



import os
def list_files(startpath):
dircount = 0
filecount = 0
for root, dirs, files in os.walk(startpath):
dircount += len(dirs)
filecount += len(files)
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print("D"+'/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
path = os.path.join(root, f)
size = os.stat(path).st_size
print("F"+' ( bytes)'.format(subindent, f, size))
print(" files in folders".format(filecount, dircount))

list_files('.')





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357467%2fpython-file-tree-size-and-countering-number-of-files-and-directories%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This solution to the problem is:



    I have added a counter for the file and the directories which start both at 0. In the forloop we add the length of the dir so the total can be calculated.



    print(" files in folders".format(filecount, dircount))



    import os
    def list_files(startpath):
    dircount = 0
    filecount = 0
    for root, dirs, files in os.walk(startpath):
    dircount += len(dirs)
    filecount += len(files)
    level = root.replace(startpath, '').count(os.sep)
    indent = ' ' * 4 * (level)
    print("D"+'/'.format(indent, os.path.basename(root)))
    subindent = ' ' * 4 * (level + 1)
    for f in files:
    path = os.path.join(root, f)
    size = os.stat(path).st_size
    print("F"+' ( bytes)'.format(subindent, f, size))
    print(" files in folders".format(filecount, dircount))

    list_files('.')





    share|improve this answer



























      0














      This solution to the problem is:



      I have added a counter for the file and the directories which start both at 0. In the forloop we add the length of the dir so the total can be calculated.



      print(" files in folders".format(filecount, dircount))



      import os
      def list_files(startpath):
      dircount = 0
      filecount = 0
      for root, dirs, files in os.walk(startpath):
      dircount += len(dirs)
      filecount += len(files)
      level = root.replace(startpath, '').count(os.sep)
      indent = ' ' * 4 * (level)
      print("D"+'/'.format(indent, os.path.basename(root)))
      subindent = ' ' * 4 * (level + 1)
      for f in files:
      path = os.path.join(root, f)
      size = os.stat(path).st_size
      print("F"+' ( bytes)'.format(subindent, f, size))
      print(" files in folders".format(filecount, dircount))

      list_files('.')





      share|improve this answer

























        0












        0








        0







        This solution to the problem is:



        I have added a counter for the file and the directories which start both at 0. In the forloop we add the length of the dir so the total can be calculated.



        print(" files in folders".format(filecount, dircount))



        import os
        def list_files(startpath):
        dircount = 0
        filecount = 0
        for root, dirs, files in os.walk(startpath):
        dircount += len(dirs)
        filecount += len(files)
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * 4 * (level)
        print("D"+'/'.format(indent, os.path.basename(root)))
        subindent = ' ' * 4 * (level + 1)
        for f in files:
        path = os.path.join(root, f)
        size = os.stat(path).st_size
        print("F"+' ( bytes)'.format(subindent, f, size))
        print(" files in folders".format(filecount, dircount))

        list_files('.')





        share|improve this answer













        This solution to the problem is:



        I have added a counter for the file and the directories which start both at 0. In the forloop we add the length of the dir so the total can be calculated.



        print(" files in folders".format(filecount, dircount))



        import os
        def list_files(startpath):
        dircount = 0
        filecount = 0
        for root, dirs, files in os.walk(startpath):
        dircount += len(dirs)
        filecount += len(files)
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * 4 * (level)
        print("D"+'/'.format(indent, os.path.basename(root)))
        subindent = ' ' * 4 * (level + 1)
        for f in files:
        path = os.path.join(root, f)
        size = os.stat(path).st_size
        print("F"+' ( bytes)'.format(subindent, f, size))
        print(" files in folders".format(filecount, dircount))

        list_files('.')






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 14:37









        Blackd00rBlackd00r

        14 bronze badges




        14 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357467%2fpython-file-tree-size-and-countering-number-of-files-and-directories%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript