what is the use of os.path.sep in python
Help with splitting a file path
python - How to split path with slashes? - Stack Overflow
"os.path.splitext(filename)" seems to be randomly assinging the split, how do I define what is filename, and what is extension?
I am learning Python from the book "Automate the Boring stuff with python:. i want to ask a doubt on page 179 what
is (os.path.sep) How we get the output by this line
calcFilePath .split(os.path.sep)
You can use os.sep
just
import os
path_string.split(os.sep)
For more info, look the doc
os.path.join(path1[, path2[, ...]]) Join one or more path components intelligently. If any component is an absolute path, all previous components (on Windows, including the previous drive letter, if there was one) are thrown away, and joining continues. The return value is the concatenation of path1, and optionally path2, etc., with exactly one directory separator (os.sep) following each non-empty part except the last. (This means that an empty last part will result in a path that ends with a separator.) Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
Use os.path.split. It is a system independent way to split paths. Note that this only splits into (head, tail). To get all the individual parts, you need to recursively split head or use str.split using os.path.sep as the separator.
I am trying to create a script that will create a dataframe of files in a directory and create two columns 'FileName' and 'Extension'.
I thought I was getting somewhere, but the output is not what I am expecting at all. Or more specifically, it is, but is an unexpected manner.
This is my current code:
import os
import pandas as pd
import pyodbc
driveO = "O:\Labels"
driveO_files = []
for filename in os.listdir(driveO):
name, file_extension = os.path.splitext(filename)
driveO_files.append({name, file_extension})
driveO_files = pd.DataFrame(driveO_files,columns=['FileName','Extension'])
print(driveO\_files.head())
btw_filesO = driveO_files.loc[driveO_files['Extension'] =='.btw']
print(btw\_filesO.head())Expected output:
| {Index} | FileName | Extension |
|---|---|---|
| 0 | LabelDesing1 | btw |
| 1 | LabelDesing1 | dat |
| 2 | LabelDesign2 | btw |
| 3 | LabelDesign2 | dat |
| 4 | LabelDesign3 | btw |
However, what I am getting is:
| {Index} | FileName | Extension |
|---|---|---|
| 0 | LabelDesing1 | btw |
| 1 | dat | LabelDesing1 |
| 2 | LabelDesign2 | btw |
| 3 | LabelDesign2 | dat |
| 4 | btw | LabelDesign3 |
It is random which items get put on which side, and changes each time I run the script.
How do I tell os.path.splitext() which side of the dot to put into which vatiable?
For what its worth:
-
There are no files that do not have an extension.
-
There is one folder within the list that always seems to go on the
'Extension'side. -
There are only 65 items in this list at present.
Any help is appreciated.
EDIT: Layout adjustments