Python: [Error 3] The system cannot find the path specified:

import os
Current_Directory = os.getcwd() # Should be ...\archive
CORPUS_PATHS = sorted([os.path.join("archive", directories) for directories in os.listdir(Current_Directory)])
filenames = []
for items in CORPUS_PATHS:
    filenames.append(sorted([os.path.join(CORPUS_PATHS, fn) for fn in os.listdir(items)]))

print filenames

I run this code from a file called archive, and there are more folders in the archive, and each of these folders has one or more text files. I want to make a list that includes the path to each of these folders. However, the following error appears.

[Error 3] The system cannot find the path specified:

I currently have a python script where I wrote this code in the same folder as the archive, and this will cause this error. What should I do to stop this error and get all file paths.

I am not good at using os, and I don't use it often, so I apologize if this is a trivial question.

Edit

import os
startpath = "archive"
corpus_path = sorted([os.path.join("archive/", directories) for directories in os.listdir(startpath)])

filenames = []
for items in corpus_path:
    print items
    path = [os.path.join(corpus_path, fn) for fn in os.listdir(items)]
    print path

, , , , . , , , , , , ,

File "C:\Users\David\Anaconda\lib\ntpath.py", line 65, in join
result_drive, result_path = splitdrive(path)

File "C:\Users\David\Anaconda\lib\ntpath.py", line 116, in splitdrive
normp = p.replace(altsep, sep)

AttributeError: 'list' object has no attribute 'replace'
+4
1

Windows. - os.listdir(). os.listdir() .

3 os.path.join( "", ). , (C: D:), : c:/archive/foo: linux "home/root/archive/foo"

- Python os.path.join Windows

os.path.join -

Windows reset, (, r '\ foo'). , reset. : drive, os.path.join( "c:", "foo" ) C: (c: foo), c:\foo.

Edit:

corpus_path [os.path.join][2] 6. ! corpus_path items.

"D:". 3 foo1, foo2 foo3. 1 2 . . . :

import os
startpath = "d:archive"
corpus_path = sorted([os.path.join("d:", "archive", directories) for directories in os.listdir(startpath)])

filenames = []
for items in corpus_path:
    print items
    path = [os.path.join(items, fn) for fn in os.listdir(items)]
    print path

:

d:archive\foo1
['d:archive\\foo1\\foo1.txt.txt', 'd:archive\\foo1\\foo11.txt']
d:archive\foo2
['d:archive\\foo2\\foo2.txt.txt']
d:archive\foo3
['d:archive\\foo3\\foo3.txt.txt']
+2

Source: https://habr.com/ru/post/1605662/


All Articles