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;
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
add a comment |
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
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
add a comment |
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
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
python python-3.x
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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('.')
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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('.')
add a comment |
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('.')
add a comment |
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('.')
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('.')
answered Mar 26 at 14:37
Blackd00rBlackd00r
14 bronze badges
14 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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