0
dirs= os.listdir('C:/Users/DELL PC/Desktop/Msc Project/MSc project/dataset')
for file in dirs:
    print (file)

lowfiles  = [f for f in os.listdir('Training data/LOW') 
             if os.path.isfile(join('Training data/LOW', f))] 
highfiles = [f for f in os.listdir('Training data/HIGH')
             if os.path.isfile(join('Training data/HIGH', f))]
files = []

I am running this code to create feature vectors but it is showing

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Training data/LOW'

While when I run the first few code to show me the directory it is showing all the files in the directory.

Why is this happening?

Gryu
  • 2,102
  • 2
  • 16
  • 29
vandana
  • 1
  • 1
  • 1
  • 1
    Are you in the directory where `Training data/LOW` exists when you run your Python program? – lurker Dec 08 '18 at 02:50
  • Python is looking for the directory `'/Training data/Low'` from the current path you are executing from. – James Dec 08 '18 at 02:50
  • What is `join` here? You should use `os.path.join`. And if "Training data" is in "C:/Users/DELL PC/Desktop/Msc Project/MSc project/dataset", you should prepend the full path. –  Dec 08 '18 at 02:55
  • you need to take care of space between Training and data. In unix you need to put "Training data" between quotes. – LOrD_ARaGOrN Dec 08 '18 at 02:57
  • 1
    @RishiBansal You don't need to add quotes in Python in such a case. –  Dec 08 '18 at 03:07

2 Answers2

1

Try this. In such cases I prefer to write an auxiliary function rather than repeatedly type (almost) the same long line (too error-prone). Likewise, if the list comprehension gets too long, I prefer the loop form.

import os

def files(path):
    result = []
    for name in os.listdir(path):
        fullname = os.path.join(path, name)
        if os.path.isfile(fullname):
            result.append(name)
    return result

path = r"C:\Users\DELL PC\Desktop\Msc Project\MSc project\dataset"
for file in os.listdir(path):
    print(file)

lowfiles = files(os.path.join(path, r"Training data\LOW"))
highfiles = files(os.path.join(path, r"Training data\HIGH"))

To clarify the comment below: the following will enter an infinite loop that prints 1, 2, 3, 1, 2, 3, 1, 2, 3...

a = [1, 2, 3]
for i in a:
    a.append(i)
    print(i)

Never add elements to a container on which you are doing a loop.

Here you are trying to do this:

for i in lowfiles:
    lowfiles.append([i, 'Low'])

So if lowfiles initially contains ["file1", "file2"], then after the first loop it will be ["file1", "file2", ["file1", "Low"]], then ["file1", "file2", ["file1", "Low"], ["file2", "Low"], [["file1", "Low"], "Low"], ...]. You don't want to do that.

I am only guessing, but it you want to rename your files by appending "Low" at the end of the name, then:

First modify the function files above to append the fullname (with directory) rather than only the name without the directory), so that you don't have to os.path.join again and again.

To rename files do the following:

for fullname in files(os.path.join(path, r"Training data\LOW")):
    os.rename(fullname, fullname + "Low")

If there is a file extension you don't want to change, you can do this:

for fullname in files(os.path.join(path, r"Training data\LOW")):
    base, ext = fullname.rsplit(".", 1)
    os.rename(fullname, base + "Low." + ext)

And if you don't want to renames files, you will have to clarify what you are trying to do.

  • Thanks a lot. I tried with your code and it is reading the folder but the next issue I am having is I am getting into a loop where the lowfile is made, So it keeps appending it with 'Low'. The same is then wrong with the files but I am never getting out of the first loop: for i in lowfiles: lowfiles.append([i, 'Low']) What should I do now? – vandana Dec 13 '18 at 22:04
  • @vandana Don't append to `lowfiles` in a loop on `lowfiles`. Python will indeed never stop this. What are you trying to do? Do you want to *rename* the files? Here `lowfiles` is a list of strings, but you are adding elements that are lists (of two strings). Not what you want, whatever it may be. –  Dec 14 '18 at 05:00
  • I have a csv file and had applied some feature extraction and wants it gives back the result in a new csv named as features.csv mypath = 'Training-Data/' csvfile = "Features/features.csv" with open(csvfile, "a") as output: writer = csv.writer(output, lineterminator='\n') writer.writerow(names) subfolder = files[counter][1] tag = files[counter][1] data_path = mypath + subfolder +'/'+files[counter][0] – vandana Dec 17 '18 at 13:38
  • @vandanaNot sure I understand. Do you want to concatenate several csv files? Here what you are writing will write file *names* to a csv file. Is it what you want? –  Dec 17 '18 at 19:35
0

I also had the same problem, tried several solutions that never worked until i got this one - maybe you can remove the for-loop section?:

import pandas as pd
import glob

path = r'C:\DRO\DCL_rawdata_files' # use your path
all_files = glob.glob(path + "/*.csv")

li = []
for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)
Ralf
  • 16,086
  • 4
  • 44
  • 68