Python Forum
Cannot redirect print to a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot redirect print to a file
#2
Backslashes are special in strings, but regular slashes are not. You don't need to double them up.

Is it creating the file?

I'd prefer to see you store the string in a variable so you don't have to create it twice.

output = "".join(islice(f, index - 5, index + 4))
print(output)
outfile.write(output)
Reply


Messages In This Thread
Cannot redirect print to a file - by tester_V - Sep-04-2020, 12:02 AM
RE: Cannot redirect print to a file - by bowlofred - Sep-04-2020, 02:06 AM
RE: Cannot redirect print to a file - by micseydel - Sep-04-2020, 04:01 AM
RE: Cannot redirect print to a file - by tester_V - Sep-11-2020, 12:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  redirect STDIO in the Python code Skaperen 6 1,335 Jul-05-2023, 12:23 AM
Last Post: Skaperen
  Saving the print result in a text file Calli 8 1,891 Sep-25-2022, 06:38 PM
Last Post: snippsat
  failing to print not matched lines from second file tester_V 14 6,194 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,239 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 11,088 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 1,834 Oct-17-2021, 05:08 AM
Last Post: davidm
  Why it does not print(file.read()) Rejaul84 1 2,363 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  stderr redirect to file fmr300 2 3,639 Apr-03-2021, 01:31 AM
Last Post: fmr300
  get two characters, count and print from a .txt file Pleiades 9 3,432 Oct-05-2020, 09:22 AM
Last Post: perfringo
  redirect url_for passing arguments with the url Leon79 1 1,680 Jul-09-2020, 05:20 PM
Last Post: Leon79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020