Python Forum
How to double quote filenames (spaces)?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to double quote filenames (spaces)?
#1
Hello,

I need to build a command and pass it to an external application.

Filenames may have spaces in them, so I must double quote them, but I can't figure out how:

fullstring="-t "
files = sys.argv[1]
output = sys.argv[2]

for filename in glob(files):
	#BAD fullstring = fullstring + f" -i gpx -f '{filename}'"
	#BAD fullstring = fullstring + f" -i gpx -f \"{filename}\""
	#BAD fullstring = fullstring + f" -i gpx -f ""{filename}"""
fullstring = f"gpsbabel {fullstring} -x track,discard -o gpx -F {output}"
print(fullstring)
subprocess.run(fullstring)
FWIW, I'm using Windows, but users might run this script on other OS's.

Thank you.
Reply
#2
Hi,
so you want to append strings like this -i gpx -f "some\file name.txt" to the fullstring, right? Or do you want to append strings like this: -i gpx -f ""some/file name.txt"".
If you want to append one of these cases just surround the new string with single quotes, so do something like this:
fullstring = fullstring + f' -i gpx -f "{filename}"'
I hope I understood you correctly
Reply
#3
Simple enough :-)

Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Next/Prev file without loading all filenames WilliamKappler 9 441 Apr-12-2024, 05:13 AM
Last Post: Pedroski55
  Very simple question about filenames and backslashes! garynewport 4 1,915 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  python sql query single quote in a string mg24 1 1,042 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,505 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Getting a list of filenames in a directory DavidHT 2 7,889 Feb-03-2020, 06:56 AM
Last Post: DavidHT
  escape single quote deep_logic 1 1,792 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  single quote problem Rikk 2 2,503 May-23-2018, 01:03 PM
Last Post: Rikk
  Escaping whitespace and parenthesis in filenames jehoshua 2 9,718 Mar-21-2018, 09:12 AM
Last Post: jehoshua
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,312 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020