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]))





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






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)