Python: get the names of specific file types in a given folder

FaCoffee

I have a folder which contains n csv files, and they are all named event_1.csv, event_2.csv, all the way up to event_n.csv.

I want to be able to raw_input their folder path and then retrieve their names, file extension excluded.

I tried to do it but I only get printed the folder path as many times as there are csv files in it. The filenames are never printed, instead, and they are what I want.

This is the best I have come up with so far, but it does the wrong thing:

import os
directoryPath=raw_input('Directory for csv files: ')
for i,file in enumerate(os.listdir(directoryPath)):
    if file.endswith(".csv"):
        print os.path.basename(directoryPath)

where directoryPath=C:\Users\MyName\Desktop\myfolder, in which there are three files: event_1.csv, event_2.csv, event_3.csv.

What I get is:

myfolder
myfolder
myfolder

What I want, instead, is:

event_1
event_2
event_3

PS: I expect to have as many as 100 files. Therefore I should be able to include situations like these, where the filename is longer:

event_10
event_100

EDIT

What can I add to make sure the files are read exactly in the same order as they are named? This means: first read event_1.csv, then event_2.csv, and so forth until I reach event_100.csv. Thanks!

Shai Efrati

It seems like you are getting what you ask for. See this line in your code:

print os.path.basename(directoryPath)

It prints the directoryPath.

I think it should be:

import os
directoryPath=raw_input('Directory for csv files: ')
for i,file in enumerate(os.listdir(directoryPath)):
    if file.endswith(".csv"):
        print os.path.basename(file)

Good luck!

EDIT:

Let's create a list of all file names without path and extension (l). Now:

for n in sorted(l, key=lambda x: int(x.split('_')[1])):
    print n

Now you need to write your specific solution :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get file names of tarred folder contents in Python

From Dev

Rename file names in a folder - Python

From Dev

Get first file of given extension from a folder

From Dev

Download a file to a specific folder with python

From Dev

Python: how to create a list with names based on the content of a given folder?

From Dev

Extracting specific file names and putting them into the folder of their name

From Dev

How to get the latest folder that contains a specific file of interest in Linux and download that file using Paramiko in Python?

From Java

Get folder name of the file in Python

From Dev

Is there a way to set a default application to open all file types for a specific folder?

From Dev

Python sort by date - specific file names

From Dev

how to get file names and paths based on a given attribute in parent tag

From Dev

How do i get images file name from a given folder

From Dev

How do i get images file name from a given folder

From Dev

Get File names from assets folder for files only with .txt extension

From Dev

Get all file names from resource folder in java project

From Dev

Get all file names in src folder, codenameone project

From Dev

Get list of file names in folder/directory with Excel VBA

From Dev

How to get all file names under a folder set into enum in Rails?

From Java

how to get only the specific return type and method names without fully qualified return types and method names in java

From Dev

Open a file with Python with given name in a folder that change name

From Dev

list file names from a folder to a tkinter window, with python 3

From Dev

python find all file names in folder that follows a pattern

From Dev

list file names from a folder to a tkinter window, with python 3

From Dev

python find all file names in folder that follows a pattern

From Dev

Moving oldest file names with different extensions to common folder. Python

From Dev

Python folder names in the directory

From Dev

How can I get a specific folder with a specific folders sibling in python

From Dev

How can I get a specific folder with a specific folders sibling in python

From Dev

How to rename the file names in a folder with new names of text file line by line in python

Related Related

  1. 1

    Get file names of tarred folder contents in Python

  2. 2

    Rename file names in a folder - Python

  3. 3

    Get first file of given extension from a folder

  4. 4

    Download a file to a specific folder with python

  5. 5

    Python: how to create a list with names based on the content of a given folder?

  6. 6

    Extracting specific file names and putting them into the folder of their name

  7. 7

    How to get the latest folder that contains a specific file of interest in Linux and download that file using Paramiko in Python?

  8. 8

    Get folder name of the file in Python

  9. 9

    Is there a way to set a default application to open all file types for a specific folder?

  10. 10

    Python sort by date - specific file names

  11. 11

    how to get file names and paths based on a given attribute in parent tag

  12. 12

    How do i get images file name from a given folder

  13. 13

    How do i get images file name from a given folder

  14. 14

    Get File names from assets folder for files only with .txt extension

  15. 15

    Get all file names from resource folder in java project

  16. 16

    Get all file names in src folder, codenameone project

  17. 17

    Get list of file names in folder/directory with Excel VBA

  18. 18

    How to get all file names under a folder set into enum in Rails?

  19. 19

    how to get only the specific return type and method names without fully qualified return types and method names in java

  20. 20

    Open a file with Python with given name in a folder that change name

  21. 21

    list file names from a folder to a tkinter window, with python 3

  22. 22

    python find all file names in folder that follows a pattern

  23. 23

    list file names from a folder to a tkinter window, with python 3

  24. 24

    python find all file names in folder that follows a pattern

  25. 25

    Moving oldest file names with different extensions to common folder. Python

  26. 26

    Python folder names in the directory

  27. 27

    How can I get a specific folder with a specific folders sibling in python

  28. 28

    How can I get a specific folder with a specific folders sibling in python

  29. 29

    How to rename the file names in a folder with new names of text file line by line in python

HotTag

Archive