[Python] Get the files in a folder with Python

I needed to get the XML in the folder and parse it, so I tried to get the file path.

Library

Import a library called glob. os is a library required to get the file name.

import glob
import os

Folder specification

Specify all the files in the folder with * (asterisk).

file = glob.glob("C:/Users/user/*")

Since we will get the XML this time, we can get the file path like this.

print(file)
['C:/Users/user/AAAAAA.xml', 'C:/Users/user/BBBBBB.xml']

Get file name

I want to use a unique file name when exporting to csv, so I also get the file name. First, get AAAAAA.xml from the file path list. Then I used os.path.split to get the filename.

file_A = file[0] //Get the top file path
filename = os.path.split(file_A)[1]

Now you can get the file name.

print(filename)
AAAAAA.xml

Folders are irregular in time and the number of XML files is unpredictable, so you really need to loop. I will post about the loop next time.

Recommended Posts

[Python] Get the files in a folder with Python
How to get the files in the [Python] folder
Get a list of files in a folder with python without a path
Process the files in the folder in order with a shell script
How to get a list of files in the same directory with python
Get the caller of a function in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
A memo organized by renaming the file names in the folder with python
Get the file name in a folder using glob
[Python] Get the numbers in the graph image with OCR
Get the result in dict format with Python psycopg2
Get the desktop path in Python
Get the weather with Python requests
Get the weather with Python requests 2
Get the script path in Python
Read files in parallel with Python
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
Get the value of a specific key in a list from the dictionary type in the list with Python
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Write the test in a python docstring
[Python] Get the variable name with str
Display Python 3 in the browser with MAMP
Search the maze with the python A * algorithm
Read all csv files in the folder
Run the Python interpreter in a script
How to get a stacktrace in python
Download files on the web with Python
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Get the EDINET code list in Python
How is the progress? Let's get on with the boom ?? in Python
Output the contents of ~ .xlsx in the folder to HTML with Python
Read a file in Python with a relative path from the program
Get the formula in an excel file as a string in Python
Solve the subset sum problem with a full search in Python
Get the number of readers of a treatise on Mendeley in Python
I made a class to get the analysis result by MeCab in ndarray with python
Get a capture of the entire web page in Selenium Python VBA
Get the weather in Osaka via WebAPI (python)
Load the network modeled with Rhinoceros in Python ③
Get a ticket for a theme park with python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Get a list of packages installed in your current environment with python
[Automation] Extract the table in PDF with Python
How to get the date and time difference in seconds with python
Create a virtual environment with conda in Python
Make a copy of the list in Python
Get only the subclass elements in a list
Get the X Window System window title in Python
Handle zip files with Japanese filenames in Python 3
Character encoding when dealing with files in Python 3
Load the network modeled with Rhinoceros in Python ②
Get and convert the current time in the system local timezone with python
Get date in Python
I get a Python No module named'encodings' error with the aws command
Receive a list of the results of parallel processing in Python with starmap