White space in batch file and shelve saving in a different directory
White space in batch file and shelve saving in a different directory
I am having problems with Project Multiclipboard from Chapter 8 of the book: Automate the Boring Stuff and using Python 3.
The first issue is that, suppose my program mcb.pyw
is saved in:
mcb.pyw
C:UsersmyNamefolder name
where the last folder has a space in the name, my batch file:
@pyw.exe C:UsersmyNamefolder namemcb.pyw %*
doesn't seem to work properly from the command line. I can now type in
mcb save keyword
into the command line without getting an error, but it's not doing anything. After testing by changing the directory to a folder whose path has no space in it, I've concluded that the problem is because of the space, but I am unsure of how I might go about fixing this.
The second issue is that when the batch file is working, the module shelve seems to be saving the data in the wrong folder. Specifically, I noticed that if I were to run mcb.pyw from the command line, shelve would save the data in C:UsersmyName
, which is also the default directory when you open the command windod, instead of the folder C:UsersmyNamefolderName
, where mcb.pyw
and mcb.bat
are saved.
C:UsersmyName
C:UsersmyNamefolderName
mcb.pyw
mcb.bat
I have gotten around this by including the lines:
import os
os.chdir('C:\Users\myName\folderName')
However, is there any other way to solve this issue? Why is shelve saving in C:UsersmyName
instead of the folder where everything is already saved?
C:UsersmyName
I apologise if I have made any ettiquette or formatting problems. If you let me know what I did wrong I will do my best to fix it as soon as I can, thank you!
1 Answer
1
Files are always saved in the current working directory unless they are specified with path names, so you do have to change your working directory if the default one is not what you want.
You can avoid hard-coding the path name and always change your working directory where the script is located with:
import os
import sys
os.chdir(os.path.dirname(sys.argv[0]))
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you for the information and suggestion! This works wonderfully! However, I'm still wondering why the current working directory and the directory where the .pyw and .bat files are saved are different. Do you know why?
– user366818
Aug 26 at 3:30