0

I am using pickle to store variables as:

pickle.dump(database,open("save.p", "wb"))

Here database is the variable (type dictionary) I am trying to store and save.p is the destination file.

Now I want to replace this as pickle.dump(database,open("%s"%var, "wb"))

Here var is taken from stdin. Basically letting the user specify the file. However just writing this does not do the trick.

The pickle documentation suggests that a new file for the given name will be created if not found in the specified path.

What I have tried:

pickle.dump(database,open(var, "wb")) to see if it can access the variable directly and pickle.dump(database,open("%s", "wb")%var).

I am missing something very obvious here. But can't figure out what.

EDIT: I suppose there was some error with permissions. I now create the file with write permissions before running the script and it works. Thank you everyone.

martineau
  • 119,623
  • 25
  • 170
  • 301
john
  • 335
  • 3
  • 12
  • How do you take `var` from the stdin? – Willem Van Onsem Jun 18 '17 at 23:50
  • @WillemVanOnsem command line argument arg =sys.argv var =str (arg[1]) – john Jun 18 '17 at 23:51
  • What does `print("%s"%var)` display? **How** does what you have fail? – martineau Jun 18 '17 at 23:54
  • @martineau The entered name as expected – john Jun 18 '17 at 23:58
  • How exactly does your initial attempt not work? – martineau Jun 19 '17 at 00:03
  • @martineau by initial attempt do you mean this : pickle.dump(database,open("%s"%var, "wb")) ? It doent work because there is no file created as expected – john Jun 19 '17 at 00:06
  • Yes, that's what I meant. So, with no error messages or exceptions, it could be creating or overwriting a file of that name in the current directory—maybe you're looking in the wrong folder. Try printing `os.getcwd()` just before calling `pickle.dump()`. – martineau Jun 19 '17 at 00:08
  • @martineau. no that is unlikely. Because when I enter the file as string and not variable and that file doesnt exist it creates one in the same directory itself. Do you thing using variables will cause it to change the path – john Jun 19 '17 at 00:13
  • No, it shouldn't change the current working directory. Try supplying the complete path to the file in the `open()` call. i.e. `open("/path/to/folder/%s"%var, "wb")`. – martineau Jun 19 '17 at 00:16
  • 1
    Not sure if it is related to your current problem but you should generally make sure that files are also closed again. In this case, use: `with open(var, 'wb') as ostr: pickle.dump(database, ostr);` – 5gon12eder Jun 19 '17 at 00:22
  • @5gon12eder: Good point, although it should be `with open("%s"%var, "wb") as ostr: ...` to avoid too many differences from the original code. – martineau Jun 19 '17 at 00:24
  • @martineau What's the point of using a format string here? Did I misunderstand that `var` already is a string? – 5gon12eder Jun 19 '17 at 00:26
  • @5gon12eder: The point is when you're doing an experiment to only change one thing at a time so you know what's causing the change in results, if any, that are seen—it's called the "scientific method" I believe. – martineau Jun 19 '17 at 00:28
  • @5gon12eder . Thanks but this does not work – john Jun 19 '17 at 00:48
  • 1
    @john I'm afraid that your question is a bit stuck at this point. There is a lot of guessing going on and nobody seems to be sure what your actual problem is. Could you please try to provide a [mcve]? – 5gon12eder Jun 19 '17 at 00:50
  • john: Did you try my suggestion of supplying the complete path to the file? What happened? – martineau Jun 19 '17 at 02:20
  • 1
    @martineau The path of the of the file did not help. I will post the code and question in more detail as 5gon12eder suggested – john Jun 19 '17 at 02:49

0 Answers0