Reputation: 1
I got an error."must be str, not list".
import os
import shutil
tesst = onlyfiles
tesst = []
with open('C:/Users/eGeneration Lab/Documents/project/try/data/final.txt') as f:
found = 2
for line in f:
if tesst in onlyfiles:
found = 1
if found == 1:
shutil.move('C:/Users/eGeneration Lab/Documents/project/try/data/'+tesst,'C:/Users/eGeneration Lab/Documents/project/try/programs/error/'+tesst)
else:
shutil.move('C:/Users/eGeneration Lab/Documents/project/try/data/'+tesst,'C:/Users/eGeneration Lab/Documents/project/try/programs/working/'+tesst)
with open("C:/Users/eGeneration Lab/Documents/project/try/data/final.txt", "a") as f:
f.write(tesst+"\n")
TypeError: must be str, not list
Upvotes: 0
Views: 2349
Reputation: 6331
If you are using python3.6+, the code can be as follows:
import shutil
from pathlib import Path
BASE_DIR = "C:/Users/eGeneration Lab/Documents/project/try"
data_path = Path(BASE_DIR) / "data"
error_path = Path(BASE_DIR) / "programs" / "error"
working_path = Path(BASE_DIR) / 'programs' / 'working'
final_file = data_path / "final.txt"
tesst = onlyfiles
tesst = []
with final_file.open() as fp:
found = 2
for line in fp:
if tesst in onlyfiles:
found = 1
if found == 1:
shutil.move(data_path / tesst, error_path / tesst)
else:
shutil.move(data_path / tesst, working_path / tesst)
with final_file.open("a") as fp:
fp.write(f"{tesst}\n")
Upvotes: 0
Reputation: 4434
tesst = []
f.write(tesst+"\n") #Error occurs: TypeError: must be str, not list
f.write("".join(tesst)+"\n") # working [list to string]
f.write("\n".join(tesst)) #is better insert `\n`
Upvotes: 1