-3

Trying to read a .csv file content from folder. Example code:

Files_in_folder = os.listdir(r"\\folder1\folder2")
Filename_list = []

For filename in files_in_folder:
    If "sometext" in filename:
         Filename_list.append(filename)

Read_this_file = "\\folder1\folder2"+max(filename_list)

Data = pandas.read_csv(Read_this_file,sep=',')

Fetching the max filename works, but the Data variable fails:

FileNotFoundError: no such file or directory.

I am able to access the folder as we see in my first line of code, but when I combine two strings, putting the r in front doesn't work, any ideas?

martineau
  • 99,260
  • 22
  • 139
  • 249
  • Try printing your `Read_this_file`, you'll understand the issue – Yevhen Kuzmovych Feb 15 '21 at 16:32
  • 2
    Look into the Python os path library. That's how you want to assemble file paths, rather than string addition. https://docs.python.org/3/library/os.path.html – mcsoini Feb 15 '21 at 16:33
  • 2
    Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. Also, please explain exactly what you don't understand from the output you get when you trace this code. Include that trace in your MRE. – Prune Feb 15 '21 at 16:33
  • Please post real code when asking a question — what you have isn't even valid Python syntax. – martineau Feb 15 '21 at 16:33
  • @Yevhen Kuzmovych: Please stop syntactically correcting the code in the OP's question. If you have an answer, post it as such. – martineau Feb 15 '21 at 16:35
  • Thank you @mcsoini, will look into that. – statistical_student Feb 15 '21 at 16:39
  • @martineau I'm correcting the obvious typos not related to OP's real problem. If you just wanted to comment that this is not a valid python code, you're not helping anybody nor following the philosophy of SO (IMO) – Yevhen Kuzmovych Feb 15 '21 at 16:39
  • @Yevhen: I know what you're trying to do—that's just not the way to do it. I've been on SO a long time, and am very familiar with its "philosophy", thank you. – martineau Feb 15 '21 at 16:41
  • @martineau, instead of pointing out my mistakes, why not try to suggest how to solve the actual problem. Clearly everyone posting here isn't going to have perfect valid python syntax, this is why we are here in the first place. – statistical_student Feb 15 '21 at 16:44
  • statistical_student: Small imperfections are fine, but the code in your question isn't even close. If you want to get help here, please make a little more effort when posting your questions. – martineau Feb 15 '21 at 16:48

2 Answers2

0

You need to add \ to your path when concatenating:

read_this_file = '\\folder1\\folder2\\' + max(filename_list)

But a better way to avoid that problem is to use

os.path.join("\\folder1\\folder2", max(filename_list))
Yevhen Kuzmovych
  • 5,476
  • 3
  • 19
  • 36
0

for a working code, use this:


files_in_folder = os.listdir("folder1/folder2/")
filename_list = []
for filename in files_in_folder:
    if "sometext" in filename:
         filename_list.append(filename)
read_this_file = "folder1/folder2/"+max(filename_list)
data = pd.read_csv(read_this_file,sep=',')

Explanation:

When you put r before a string, the character following a backslash is included in the string without change, and all backslashes are left in the string.

In your example, if you try to print "\folder1\folder2" Python will read the '\f' part as a special character (just as it would for \n for example).

Dharman
  • 21,838
  • 18
  • 57
  • 107