Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newby to Pyhon
#1
Hi, I am new to this forum and new to Python. I am starting with some very basic scripting to manipulate and manage files. Eventually I want to setup some automated tasks to reduce workload.

Can anyone help me with my commands. I've managed to crate a text file and write into it but I cannot open the file and return the data within it via print.

I'm not sure where I'm going wrong, any comments would be appreciated.

I'm running Python 3.6.2 via a shell on a Windows operating system.

Thanks

>>> data = open("readit.txt", "r")
>>> print data.read(1)
SyntaxError: invalid syntax
>>> data.close()
>>> file = open("writefile.txt", "w")
>>> file.write("hello")
5
>>> file.close()
>>> read = open("writefile.txt", "r")
>>> print read.line(1)
SyntaxError: invalid syntax
>>> print read.readline
SyntaxError: Missing parentheses in call to 'print'
>>> print read.line(0)
SyntaxError: invalid syntax
>>> print read.read()
SyntaxError: invalid syntax
>>> 
Reply
#2
print has become a function in Python 3,always print().
Example print(data.read(1))print(read.read())
Reply
#3
with open('file.txt', 'r') as out_file:
     print(out_file.write('hello'))
That way you don't have to use close() after opening the file. It closes automaticaly. The with statement is called context manager.

Writing to a file:
with open('file.txt', 'w') as in_file:
    in_file.write('hello')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Aug-31-2017, 08:30 AM)snippsat Wrote: print has become a function in Python 3,always print().
Example print(data.read(1))print(read.read())

Thanks that worked straight away. Now onto loops and search strings Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pass arguments from bat file to pyhon script from application absolut 2 1,103 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Calling a C Func though Pyhon Script using DLL file which is created for C file. CMMouli 4 5,665 Feb-18-2017, 05:06 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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