0

I'm currently playing around with making a file duplicator in Python. It currently works on its own when I customise the number of duplications in the actual program, but I am working on making a separate and basic setup file. This will allow you to specify the number of duplicates you want.

At the moment, this number is saved onto a plain text document in list form. So the document can look like [0, 1, 2, 3] to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] depending on what you put in.

The problem is that when the actual duplicator program tries to get the list from the plain text file, it interprets it as a string, so it looks like

count = ['[0, 1, 2, 3, 4, 5]']

I have searched the internet far and wide for a solution to this, but the only solution I can find is to use str.split(). From my knowledge, this was removed in Python 3. Are there any alternatives that I can employ in this situation?

Here is the code for the actual duplicator:

#!/usr/bin/env python
import shutil
import getpass
data = open("/Volumes/USB/Data.txt", "r")
username = getpass.getuser()
count = data.readlines(1)
print(str(count))
for x in count:
    shutil.copy2('/Volumes/USB/img.jpg', 'img{0}.jpg'.format(x))
    print(str(count))

And here is the code for the setup file:

import time
import fileinput
with open("/Volumes/USB/Data,txt", "r") as f:
# Precount is where the original list will go to let them know how many have previously been selected. I will convert this to the number of entries in the list eventually.
    precount = list(f.readlines(1))
    print('Enter the desired amount of images. Your current amount is {0}.'.format(precount))
countbase = input()
count = [0]
n = 1
while int(n) < int(countbase):
    list.append(count, n)
    n += 1
with open("/Volumes/USB/Data.txt", "w") as f:
    f.write(str((count)))
print('Configuration complete!')
time.sleep(1)
quit()

Thanks for any help!

juanpa.arrivillaga
  • 65,257
  • 7
  • 88
  • 122

2 Answers2

0

You would probably be happier with next(data), but what you have currently is a very short list returned by .readlines(1).

If you take the initial list element you'll just have a string:

count[0]

You can pass that to a JSON parser to turn the string into a list of integers:

import json
counts = json.loads(count[0])

Then you can iterate over those ints to your heart's content.

J_H
  • 9,521
  • 1
  • 16
  • 31
0

There are a few ways to do this.

The first, and simplest, would be to use the ast module to parse the input:

import ast

ast.literal_eval('[1, 2, 3, 4, 5]')

This produces the list object [1, 2, 3, 4, 5].

If you foresee adding more options, it would probably be better to store your configuration data in JSON format. You can use the functions in the json module for that (in particular, json.dump and json.load).

Lastly, you could use the pickle module to save your configuration in binary format. I wouldn't recommend this, because pickle allows arbitrary code execution.

gmds
  • 16,465
  • 4
  • 22
  • 45