zsad512
zsad512

Reputation: 1177

How to extract the file name from a file path?

I have the following code:

os.listdir("staging")

# Seperate filename from extension
sep = os.sep

# Change the casing
for n in os.listdir("staging"):
    print(n)
    if os.path.isfile("staging" + sep + n):
        filename_one, extension = os.path.splitext(n)
        os.rename("staging" + sep + n, "staging" + sep + filename_one.lower() + extension)

# Show the new file names
print ('\n--------------------------------\n')
for n in os.listdir("staging"):
    print (n)

# Remove the blanks, -, %, and /
for n in os.listdir("staging"):
    print (n)
    if os.path.isfile("staging" + sep + n):
        filename_zero, extension = os.path.splitext(n)
        os.rename("staging" + sep + n , "staging" + sep + filename_zero.replace(' ','_').replace('-','_').replace('%','pct').replace('/','_') + extension)

# Show the new file names
print ('\n--------------------------------\n')
for n in os.listdir("staging"):
    print (n)

"""
In order to fix all of the column headers and to solve the encoding issues and remove nulls, 
first read in all of the CSV's to python as dataframes, then make changes and rewrite the old files
"""
import os
import glob
import pandas as pd

files = glob.glob(os.path.join("staging" + "/*.csv"))

print(files)

# Create an empty dictionary to hold the dataframes from csvs
dict_ = {}

# Write the files into the dictionary
for file in files:
    dict_[file] = pd.read_csv(file, header = 0, dtype = str, encoding = 'cp1252').fillna('')

In the dictionary, the dataframes are named as "folder/name(csv)" what I would like to do is remove the prefix "staging/" from the keys in the dictionary.

How can I do this?

Upvotes: 30

Views: 85837

Answers (5)

basil_man
basil_man

Reputation: 604

In the same spirt as truncate the file paths, use pathlib in python standard library. It will turn the path into an easy to use class.

from pathlib import Path

path = Path('Desktop/folder/test.txt')

path.name    # test.txt
path.stem    # test
path.suffix  # .txt
path.parent.name # folder
path.parent.parent.name # Desktop

Upvotes: 10

cs95
cs95

Reputation: 402483

If all you want to do is truncate the file paths to just the filename, you can use os.path.basename:

for file in files:
    fname = os.path.basename(file)
    dict_[fname] = (pd.read_csv(file, header=0, dtype=str, encoding='cp1252')
                      .fillna(''))

Example:

os.path.basename('Desktop/test.txt')
# 'test.txt'

Upvotes: 56

Justin Malinchak
Justin Malinchak

Reputation: 557

import os
pathname ='c:\\hello\\dickins\\myfile.py'
head, tail = os.path.split(pathname)
print head
print tail

Upvotes: 8

Santhosh
Santhosh

Reputation: 1672

This article here worked out just fine for me

import os
inputFilepath = 'path/to/file/foobar.txt'
filename_w_ext = os.path.basename(inputFilepath)
filename, file_extension = os.path.splitext(filename_w_ext)
#filename = foobar
#file_extension = .txt

path, filename = os.path.split(path/to/file/foobar.txt)
# path = path/to/file
# filename = foobar.txt

Hope it helps someone searching for this answer

Upvotes: 4

JerryPlayz101
JerryPlayz101

Reputation: 117

As ColdSpeed said, you can use "os.path.basename" to truncate a file to its name, but I think what you are refering to is the ability to pycache the data?

For Example here is my Directory: My Directory for the Game I'm Making Atm In the Assets folder..

You see the pycache folder? that initializes it as a module. Then, you can import a file from that module (for example the staging.txt file and operate on it.) For Example I use the IpConfig.txt File from the assets folder level (or should be) and take a line of information out of it.

import pygame as pyg
import sys
import os
import math
import ssl
import socket as sock
import ipaddress as ipad
import threading
import random
print("Modules Installed!")

class two:
    # Find out how to refer to class super construct
    def main(Display, SecSock, ipadd, clock):
        # I have code here that has nothing to do with the question...


    def __init__():
        print("Initializing[2]...")
        # Initialization of Pygame and SSL Socket goes here

        searchQuery = open("IpConfig.txt", 'r') #Opening the File IpConfig(Which now should open on the top level of the game files)

        step2 = searchQuery.readlines()# read the file
        ipadd = step2[6] # This is what you should have or something similar where you reference the line you want to copy or manipulate.

        main(gameDisplay, SSLSock, ipadd, clock)# Im having issues here myself - (main() is not defined it says)
        print(ipadd)
        print("Server Certificate Configuration Enabled...")








    __init__() # Start up the procedure

Upvotes: 0

Related Questions