简体   繁体   中英

If statement causes things to fail

I need to read data from a set of files in a folder. Certain fields are updated using the filename as a reference. This will be appended to a larger dataframe to export to an excel.

When I run this code without the "if" it gives the expected result. It only throws an error on encountering a folder. So I introduced the if to check if its a file. Now it throws no error but also give no output.

import pandas as pnd
import os
from os import listdir
from os.path import isfile, join
MyPath= "H:\Folder\Trial"
for CurrentFile in listdir(MyPath):
    if os.path.isfile(CurrentFile):
        .....Read some data.

I need to read data from the files in this folder. But right not it does nothing

First get full path of file and then use isfile function, see below lines: Also use r before mypath string which will use it as raw string or double \\ instead of single \\.

MyPath= r"H:\Folder\Trial"
for CurrentFile in listdir(MyPath):
    filepath = os.path.join(MyPath, CurrentFile)
    if os.path.isfile(filepath):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM