2017-07-15 135 views
2

我有以下代碼:如何從文件路徑中提取文件名?

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('') 

在詞典中,dataframes被命名爲「文件夾/名稱(CSV)」我希望做的是從鍵刪除前綴「分期/」詞典。

我該怎麼做?

回答

4

如果你想要做的是截斷的文件路徑,只是文件名,你可以使用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('') 

例子:

In [465]: os.path.basename('Desktop/test.txt') 
Out[465]: 'test.txt' 
+0

@COLDSPEED,我不知道如何執行你的suggestion-我有61鍵在字典中,他們被標記爲「staging /文件名」我想將鍵改爲「文件名」 - 我試着用files = glob.glob(os.path.join(「staging」+「/ * .csv「)) print(files) 對於文件中的文件: file = os.path.basename(file) print(files) – zsad512

+0

請看如何提供[mcve]。 –

0

正如ColdSpeed說,你可以使用「操作系統.path.basename「截斷文件到它的名字,但我認爲你所指的是pycache數據的能力?

例如,下面是我的目錄: My Directory for the Game I'm Making Atm In the Assets folder..

你看pycache文件夾?將其初始化爲一個模塊。 然後,您可以從該模塊導入文件(例如staging.txt文件並對其進行操作)。 For Example 我使用資產文件夾級別(或應該是)的IpConfig.txt文件,並佔用一行信息。

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

感謝您的詳細迴應,但@COLDSPEED得到它的權利,我需要做的只是截斷文件名插入到數據庫 – zsad512

+0

我只是澄清你將如何去閱讀該數據庫。 – JerryPlayz101

1

article在這裏工作了就好了,我

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 

希望它可以幫助別人尋找這個答案