0

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
asked May 15, 2019 at 5:28
4
  • Maybe you should cast the object to a string? Anyway, your question is not clear at all... Commented May 15, 2019 at 5:31
  • 1
    Can you please share the entire stacktrace? Commented May 15, 2019 at 5:36
  • you are writing a list to a file you can only write strings to a file convert the list to string in whichever format you want just str(tesst) might work now but not applicable for every case Commented May 15, 2019 at 5:40
  • Your code makes no sense at all. Tesst is overwriten by an empty list; whereas the 'tesst' in first place refers to something that is not even in your code script. So either include that "onlyfiles", explain what it does or is and how we should handle it or just post some sort of hook to use it. List to files writing is not permitted. Only strings. You can do for x in tesst: write x to file. Commented May 15, 2019 at 5:40

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.