Hello,
On Windows, I need to loop through all .TXT files in a directory.
In a command-line script (no GUI), I can't figure out how to use the user-provided directory, considering that the path could have spaces in them, in which case Windows will add double quotes before and after… but either Windows or Python removes the first double quote :-/
Also, the path could have a trailing backslash… or not.
---
Edit: I get a different output depending on if the path includes a trailing backslash or not. And obviously, not quotting a path that holds spaces isn't a good idea…
On Windows, I need to loop through all .TXT files in a directory.
In a command-line script (no GUI), I can't figure out how to use the user-provided directory, considering that the path could have spaces in them, in which case Windows will add double quotes before and after… but either Windows or Python removes the first double quote :-/
Also, the path could have a trailing backslash… or not.
import sys import os import glob arguments = len(sys.argv) - 1 if arguments < 1: print ("Usage: myscript.py c:\some dir\some where\") exit() src = sys.argv[1] os.chdir(src) """ "c:\some dir\some where\" becomes c:\some dir\some where\" """ print(src) for filename in os.listdir(src): print(filename)Thank you.
---
Edit: I get a different output depending on if the path includes a trailing backslash or not. And obviously, not quotting a path that holds spaces isn't a good idea…
path = sys.argv[1] print(path) """ c:\>myscript.py "c:\some dir\some where\" c:\some dir\some where" c:\>myscript.py "c:\some dir\some where" c:\some dir\some where c:\>myscript.py c:\some dir\some where c:\some """