How to extract the file name from a file path?

后端 未结 4 356
我在风中等你
我在风中等你 2021-01-04 03:51

I have the following code:

os.listdir(\"staging\")

# Seperate filename from extension
sep = os.sep

# Change the casing
for n in os.listdir(\"staging\"):
           


        
相关标签:
4条回答
  • 2021-01-04 04:26

    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'
    
    0 讨论(0)
  • 2021-01-04 04:31
    import os
    pathname ='c:\\hello\\dickins\\myfile.py'
    head, tail = os.path.split(pathname)
    print head
    print tail
    
    0 讨论(0)
  • 2021-01-04 04:40

    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:

    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.) 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
    
    0 讨论(0)
  • 2021-01-04 04:48

    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

    0 讨论(0)
提交回复
热议问题