How to make python count how many files are in a directory?












-1














Just by the title, you might think that this is a duplicate but it is not. I need to make my program count how many files with a specific ending such as .mp3 or .mp4 are in a directory. So if I have 10 .mp3 files in a directory I want my program to figure that out. After that, I need to list those files with numbers next to them so that the user can enter a number to launch that file. I need help with counting the files.










share|improve this question




















  • 1




    Take a look at stackoverflow.com/questions/33635353/…
    – KJH
    Nov 23 at 1:54






  • 1




    for file in glob.glob('/some/directory/*.mp3'):
    – John Gordon
    Nov 23 at 1:57










  • Great for the first part but I still need help with the number part.
    – Hrittik Chatterjee
    Nov 23 at 1:59










  • btw I am taking KJH's answer if anyone is wondering. The sites posted provided clear answers.
    – Hrittik Chatterjee
    Nov 23 at 2:17










  • Split the filename on the dot; feed the extensions into collections.Counter.
    – wwii
    Nov 23 at 2:19
















-1














Just by the title, you might think that this is a duplicate but it is not. I need to make my program count how many files with a specific ending such as .mp3 or .mp4 are in a directory. So if I have 10 .mp3 files in a directory I want my program to figure that out. After that, I need to list those files with numbers next to them so that the user can enter a number to launch that file. I need help with counting the files.










share|improve this question




















  • 1




    Take a look at stackoverflow.com/questions/33635353/…
    – KJH
    Nov 23 at 1:54






  • 1




    for file in glob.glob('/some/directory/*.mp3'):
    – John Gordon
    Nov 23 at 1:57










  • Great for the first part but I still need help with the number part.
    – Hrittik Chatterjee
    Nov 23 at 1:59










  • btw I am taking KJH's answer if anyone is wondering. The sites posted provided clear answers.
    – Hrittik Chatterjee
    Nov 23 at 2:17










  • Split the filename on the dot; feed the extensions into collections.Counter.
    – wwii
    Nov 23 at 2:19














-1












-1








-1


0





Just by the title, you might think that this is a duplicate but it is not. I need to make my program count how many files with a specific ending such as .mp3 or .mp4 are in a directory. So if I have 10 .mp3 files in a directory I want my program to figure that out. After that, I need to list those files with numbers next to them so that the user can enter a number to launch that file. I need help with counting the files.










share|improve this question















Just by the title, you might think that this is a duplicate but it is not. I need to make my program count how many files with a specific ending such as .mp3 or .mp4 are in a directory. So if I have 10 .mp3 files in a directory I want my program to figure that out. After that, I need to list those files with numbers next to them so that the user can enter a number to launch that file. I need help with counting the files.







python directory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 2:16

























asked Nov 23 at 1:51









Hrittik Chatterjee

1510




1510








  • 1




    Take a look at stackoverflow.com/questions/33635353/…
    – KJH
    Nov 23 at 1:54






  • 1




    for file in glob.glob('/some/directory/*.mp3'):
    – John Gordon
    Nov 23 at 1:57










  • Great for the first part but I still need help with the number part.
    – Hrittik Chatterjee
    Nov 23 at 1:59










  • btw I am taking KJH's answer if anyone is wondering. The sites posted provided clear answers.
    – Hrittik Chatterjee
    Nov 23 at 2:17










  • Split the filename on the dot; feed the extensions into collections.Counter.
    – wwii
    Nov 23 at 2:19














  • 1




    Take a look at stackoverflow.com/questions/33635353/…
    – KJH
    Nov 23 at 1:54






  • 1




    for file in glob.glob('/some/directory/*.mp3'):
    – John Gordon
    Nov 23 at 1:57










  • Great for the first part but I still need help with the number part.
    – Hrittik Chatterjee
    Nov 23 at 1:59










  • btw I am taking KJH's answer if anyone is wondering. The sites posted provided clear answers.
    – Hrittik Chatterjee
    Nov 23 at 2:17










  • Split the filename on the dot; feed the extensions into collections.Counter.
    – wwii
    Nov 23 at 2:19








1




1




Take a look at stackoverflow.com/questions/33635353/…
– KJH
Nov 23 at 1:54




Take a look at stackoverflow.com/questions/33635353/…
– KJH
Nov 23 at 1:54




1




1




for file in glob.glob('/some/directory/*.mp3'):
– John Gordon
Nov 23 at 1:57




for file in glob.glob('/some/directory/*.mp3'):
– John Gordon
Nov 23 at 1:57












Great for the first part but I still need help with the number part.
– Hrittik Chatterjee
Nov 23 at 1:59




Great for the first part but I still need help with the number part.
– Hrittik Chatterjee
Nov 23 at 1:59












btw I am taking KJH's answer if anyone is wondering. The sites posted provided clear answers.
– Hrittik Chatterjee
Nov 23 at 2:17




btw I am taking KJH's answer if anyone is wondering. The sites posted provided clear answers.
– Hrittik Chatterjee
Nov 23 at 2:17












Split the filename on the dot; feed the extensions into collections.Counter.
– wwii
Nov 23 at 2:19




Split the filename on the dot; feed the extensions into collections.Counter.
– wwii
Nov 23 at 2:19












1 Answer
1






active

oldest

votes


















1














import os 
i=0
x=
for file in os.listdir():
if file.endswith('.mp3'):
print(file)
x.append(file)
i+=1
print('the total number of files: ' +str(i))
fileNumber=input('enter number')
os.startfile(x[int(fileNumber)])


make sure to use change directory to the folder location using os.chdir() or enter complete path in os.listdir()






share|improve this answer























  • I got that part the part I need help with now is adding numbers to each file and then print all the files out with those numbers. I need the numbering system so that each file is numbered in a way that a user can just enter 0 and the file labeled 0 will launch.
    – Hrittik Chatterjee
    Nov 23 at 2:29










  • Also I cant ask any more questions apparently so if you guys could upvote this :)
    – Hrittik Chatterjee
    Nov 23 at 2:30










  • you can use `os.startfile()'
    – timmy
    Nov 23 at 2:34










  • do not worry i will edit my answer to complete your answer
    – timmy
    Nov 23 at 2:35










  • i should note that programmers start count from 0
    – timmy
    Nov 23 at 2:41











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%2f53439790%2fhow-to-make-python-count-how-many-files-are-in-a-directory%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









1














import os 
i=0
x=
for file in os.listdir():
if file.endswith('.mp3'):
print(file)
x.append(file)
i+=1
print('the total number of files: ' +str(i))
fileNumber=input('enter number')
os.startfile(x[int(fileNumber)])


make sure to use change directory to the folder location using os.chdir() or enter complete path in os.listdir()






share|improve this answer























  • I got that part the part I need help with now is adding numbers to each file and then print all the files out with those numbers. I need the numbering system so that each file is numbered in a way that a user can just enter 0 and the file labeled 0 will launch.
    – Hrittik Chatterjee
    Nov 23 at 2:29










  • Also I cant ask any more questions apparently so if you guys could upvote this :)
    – Hrittik Chatterjee
    Nov 23 at 2:30










  • you can use `os.startfile()'
    – timmy
    Nov 23 at 2:34










  • do not worry i will edit my answer to complete your answer
    – timmy
    Nov 23 at 2:35










  • i should note that programmers start count from 0
    – timmy
    Nov 23 at 2:41
















1














import os 
i=0
x=
for file in os.listdir():
if file.endswith('.mp3'):
print(file)
x.append(file)
i+=1
print('the total number of files: ' +str(i))
fileNumber=input('enter number')
os.startfile(x[int(fileNumber)])


make sure to use change directory to the folder location using os.chdir() or enter complete path in os.listdir()






share|improve this answer























  • I got that part the part I need help with now is adding numbers to each file and then print all the files out with those numbers. I need the numbering system so that each file is numbered in a way that a user can just enter 0 and the file labeled 0 will launch.
    – Hrittik Chatterjee
    Nov 23 at 2:29










  • Also I cant ask any more questions apparently so if you guys could upvote this :)
    – Hrittik Chatterjee
    Nov 23 at 2:30










  • you can use `os.startfile()'
    – timmy
    Nov 23 at 2:34










  • do not worry i will edit my answer to complete your answer
    – timmy
    Nov 23 at 2:35










  • i should note that programmers start count from 0
    – timmy
    Nov 23 at 2:41














1












1








1






import os 
i=0
x=
for file in os.listdir():
if file.endswith('.mp3'):
print(file)
x.append(file)
i+=1
print('the total number of files: ' +str(i))
fileNumber=input('enter number')
os.startfile(x[int(fileNumber)])


make sure to use change directory to the folder location using os.chdir() or enter complete path in os.listdir()






share|improve this answer














import os 
i=0
x=
for file in os.listdir():
if file.endswith('.mp3'):
print(file)
x.append(file)
i+=1
print('the total number of files: ' +str(i))
fileNumber=input('enter number')
os.startfile(x[int(fileNumber)])


make sure to use change directory to the folder location using os.chdir() or enter complete path in os.listdir()







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 2:45

























answered Nov 23 at 2:26









timmy

767




767












  • I got that part the part I need help with now is adding numbers to each file and then print all the files out with those numbers. I need the numbering system so that each file is numbered in a way that a user can just enter 0 and the file labeled 0 will launch.
    – Hrittik Chatterjee
    Nov 23 at 2:29










  • Also I cant ask any more questions apparently so if you guys could upvote this :)
    – Hrittik Chatterjee
    Nov 23 at 2:30










  • you can use `os.startfile()'
    – timmy
    Nov 23 at 2:34










  • do not worry i will edit my answer to complete your answer
    – timmy
    Nov 23 at 2:35










  • i should note that programmers start count from 0
    – timmy
    Nov 23 at 2:41


















  • I got that part the part I need help with now is adding numbers to each file and then print all the files out with those numbers. I need the numbering system so that each file is numbered in a way that a user can just enter 0 and the file labeled 0 will launch.
    – Hrittik Chatterjee
    Nov 23 at 2:29










  • Also I cant ask any more questions apparently so if you guys could upvote this :)
    – Hrittik Chatterjee
    Nov 23 at 2:30










  • you can use `os.startfile()'
    – timmy
    Nov 23 at 2:34










  • do not worry i will edit my answer to complete your answer
    – timmy
    Nov 23 at 2:35










  • i should note that programmers start count from 0
    – timmy
    Nov 23 at 2:41
















I got that part the part I need help with now is adding numbers to each file and then print all the files out with those numbers. I need the numbering system so that each file is numbered in a way that a user can just enter 0 and the file labeled 0 will launch.
– Hrittik Chatterjee
Nov 23 at 2:29




I got that part the part I need help with now is adding numbers to each file and then print all the files out with those numbers. I need the numbering system so that each file is numbered in a way that a user can just enter 0 and the file labeled 0 will launch.
– Hrittik Chatterjee
Nov 23 at 2:29












Also I cant ask any more questions apparently so if you guys could upvote this :)
– Hrittik Chatterjee
Nov 23 at 2:30




Also I cant ask any more questions apparently so if you guys could upvote this :)
– Hrittik Chatterjee
Nov 23 at 2:30












you can use `os.startfile()'
– timmy
Nov 23 at 2:34




you can use `os.startfile()'
– timmy
Nov 23 at 2:34












do not worry i will edit my answer to complete your answer
– timmy
Nov 23 at 2:35




do not worry i will edit my answer to complete your answer
– timmy
Nov 23 at 2:35












i should note that programmers start count from 0
– timmy
Nov 23 at 2:41




i should note that programmers start count from 0
– timmy
Nov 23 at 2:41


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53439790%2fhow-to-make-python-count-how-many-files-are-in-a-directory%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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks