Skip to content
Advertisement

OSError: [Errno 22] Invalid argument: – Changing backslash to forward slash not helping! (Windows)

I am working with streamlit to create a tool that takes user input (csv file name) and cleans/produces output as a dataframe. I continuously get OSError: [Errno 22] Invalid argument: ‘M:/Desktop/AutomationProject/’

I am aware of all the past solves of this error, however they all say change backslash to forward slash on windows and this is a quick fix, however after doing this I still have the same issue.

Note my tool still works when inputting the file name, just consistently shows an error (below)

Thanks in advance for your help!

Code:

st.header('1 - Express Autocalls')

autocall_gbp_file = str(st.text_input("Please type in your Autocall File Name (GBP)"))

express_gbp = pd.read_csv("M:/Desktop/AutomationProject/" + autocall_gbp_file)

OSError: [Errno 22] Invalid argument: ‘M:/Desktop/AutomationProject/’ Traceback: File “C:Usersadavie18.condaenvsprojectenvlibsite->packagesstreamlitscriptrunnerscript_runner.py”, line 475, in _run_script exec(code, module.dict) File “M:DesktopAutomationProjectAutocallApp.py”, line 176, in express_gbp = pd.read_csv(“M:/Desktop/AutomationProject/” + autocall_gbp_file) File “C:Usersadavie18.condaenvsprojectenvlibsite-packagespandasutil_decorators.py”, >line 311, in wrapper return func(*args, **kwargs) File “C:Usersadavie18.condaenvsprojectenvlibsite-packagespandasioparsersreaders.py”, >line 680, in read_csv return _read(filepath_or_buffer, kwds) File “C:Usersadavie18.condaenvsprojectenvlibsite-packagespandasioparsersreaders.py”, >line 575, in _read parser = TextFileReader(filepath_or_buffer, **kwds) File “C:Usersadavie18.condaenvsprojectenvlibsite-packagespandasioparsersreaders.py”, >line 933, in init self._engine = self._make_engine(f, self.engine) File “C:Usersadavie18.condaenvsprojectenvlibsite-packagespandasioparsersreaders.py”, >line 1217, in _make_engine self.handles = get_handle( # type: ignore[call-overload] File “C:Usersadavie18.condaenvsprojectenvlibsite-packagespandasiocommon.py”, line 789, >in get_handle handle = open(

Advertisement

Answer

The usual best practice to keep OS paths consistent across platforms in pythong is using the os module:

import os

path1 = "Desktop/" + "folder1/" + "folder2/"
with open(path1, "r") as file:
    pass 
    # here, script is not consistent across OS, 
    # and can be difficult to format correctly for Windows


# instead, do:
path2 = os.path.join("Desktop", "folder1", "folder2")
with open(path2, "r") as file:
    pass 
    # now, your script can find your Windows files, 
    # and the same script works for MacOS, Linux platforms

This helps keep consistency across platforms, so you can avoid meticulous string formatting

Advertisement