I am trying to develop a python script that will iterate through several Excel .xlsx files, search each file for a set of values and save them to a new .xlsx template.
The issue I'm having is when I'm trying to get a proper list of files in the folder I'm looking at. I'm saving these filenames in a list variable 'fileList' to manage iteration.
When I run the code os.chdir(sourcepath),
I'm constantly getting a FileNotFoundError: [WinError 2] The system cannot find the file specified: C:\\Users\\username\\PycharmProjects\\projectName\\venv\\Site List\\siteListfolder
I think this has to do with the '\\' that is displaying in the error, but when I run a print(sourcepath) in this code, the path is properly displayed, with just one '\' between each subdirectory instead of two.
I need to be able to get the list of files in the siteListfolder, and be able to iterate through them using this kind of logic:
priCLLI = sys.argv[1]
secCLLI = sys.argv[2]
sourcepath = os.path.join(homepath, 'Site List', f'{priCLLI}_{secCLLI}')
siteListfolder = os.listdir(sourcepath)
for file in siteListfolder:
for row in file:
<script does its work>
'siteListfolder = os.listdir(sourcepath)' is generating the error Thanks to all in advance for supporting this kind of forum.
os.chdir(sourcepath)" - but that isn't part of the actual code you showed. "I think this has to do with the '\\' that is displaying in the error" No; that's normal - it shows you a representation of the path (a string), i.e. the same thing that you would have to type into your Python code to get the actual string. It has double backslashes because those would be needed as escape sequences in code. The problem is that the folder actually isn't there.os.listdir? Are you able to make it work if you look for parent directories of the one causing the problem? Also, this is probably just an artifact of your anonymization process; but I notice that the example path you show doesn't have an underscore insiteListfolder, but your code will clearly produce an underscore withf'{priCLLI}_{secCLLI}'.