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
Mureinik
316k54 gold badges404 silver badges406 bronze badges
2 Answers 2
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`
answered May 15, 2019 at 5:40
Wang Liang
4,4627 gold badges26 silver badges50 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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")
answered May 15, 2019 at 6:01
Waket Zheng
6,5792 gold badges23 silver badges38 bronze badges
Comments
lang-py
str(tesst)might work now but not applicable for every case