How can I open a text file with Notepad? [duplicate] - python

This question already has answers here:
Open a text file using notepad as a help file in python?
(7 answers)
Closed 3 years ago.
Hoping somebody can help me with the following.
The function works just fine:
def fileOpen(filename, accessmode):
file = open(filename, accessmode)
for line in file:
print(line)
return
The filename is "open.txt"
Instead of opening notepad the results are displayed in the python console. Notepad is not opened. I know that it can be solved with this:
import os
os.system('notepad.exe ' + 'open.txt')
But I was wondering if there is a setting in visual studio to get around my problem?
Any help would be greatly appreciated.
VisualStudio Community 2017
Python 3.6.6
Sparkington

The 'problem' is that there is no problem. You are attempting to use the print function to open notepad, not to print to the console - which is its job. This is 'solved' by using os.system() which will execute the notepad.exe program with the argument of 'open.txt'.
So I think you do not need to be looking for a solution to this problem, as there is no problem to be honest with you. You are using print to do something which is not its intended purpose, or even close to it. So is there any wonder that it doesn't work?
My suggestion to you, is to just use os.system and not to try using print for something other than its function.

Related

How to get the type of a file with python?

I want to find out the filetypes of some files with no file ending.
The best case would be if I could get the same string you get in the file properties. Iam using Python and already tried with mimetypes, which doesn't worked.
Thanks for any help :)
I think your question has been resolved in this issue on stackoverflow :
check type of file without extensions
hopes this help :)
You can try using the file command, executed via subprocess.
result = subprocess.check_output(['file', '/path/to/allcfgconv'])
The resulting string is a bit verbose; you'll have to parse the file type from it yourself.

Syntax error when trying to open a file through Python 3 Command Shell

So I am learning Python through Udemy tutorials and now I need to open a file through CMD(CMD is opened on folder I need) and when I am typing function for opening file it says syntax error, but I have made everything good what a guy on tutorials says, I really don't know what what should I do, I checked all of the forums and still cant find the answer.
Here are some screenshots:
Couple of issues:
1.Your text file is called "example.txt.txt" instead of "example.txt"
2.The "example.txt","r" should be surrounded with brackets () instead of <>. These symbols look similar in cmd and are easy to confuse.
#instead of
file = open<"example.txt","r">
#use
file = open("example.txt","r")
This should fix your problem; let me know if it does.
You have to use parenthesss () not <>
file = open("example.txt","r")
check https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

problem with with-as statement as mentioned in learn python3 the hard way [duplicate]

This question already has answers here:
What is the python "with" statement designed for?
(11 answers)
Closed 4 years ago.
I've been reading the learn python3 the hard way book and in a exercise about python symbols he refers to a 'as' symbol and in the description it says "Part of the with-as statement" and the example format is "with X as Y: pass" but i couldn't find anything about such a thing online so I'm asking here.
Does anyone know anything about it?
and as a refrence it's exercise 37
The With x as y construct in python in called a context manager.
Context managers are used to properly manage resources. For example, if one is used to open a file, a context manager will ensure the file is closed.
with open('my_file.txt', 'r') as file:
for line in file:
print('{}'.format(line))
This is equivalent to:
file = open('my_file.txt') as file
for line in file:
print('{}.format(line))
file.close()
As you can see, the call to the close function is not necessary when you use a context manager.Its easy to forget to close the file, and this can lead to your system crashing if too many files are open. (There is a maximum number allowed by the operating system.)
See this link for more information and examples.

Python3 reading and writing .txt files [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 5 years ago.
I'm somewhat new to Python, but I have enough under my belt to know what I'm doing. What I'm trying to do is write a few lines for a .txt file (as well as a variable), and then print 5 of those characters.
import os
username = "Chad_Wigglybutt"
file = open("testfile.txt", "w")
file.write("Hello .txt file, ")
file.write("This is a test, ")
file.write("Can this write variables? ")
file.write("Lets see: ")
file.write(username)
file.close()
It then creates the file without issue, but when I add
print file.read(5)
to the code, it gives me a syntax error for file.read, and I have no clue why. I've been on the internet for a few hours now and I can't find anything. Either I'm extremely bad at google searching and I'm an idiot, or something's broken, or both. Any tips/ideas? :/
You're writing Python 3 code. In Python 3, print is a function, not a special statement. You need parentheses for function calls:
print(file.read(5))

os.system not writing in terminal [duplicate]

This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 6 years ago.
i have a simple test file i created in order to use vmd (a program for my job)
This test file is as simple as :
import os
os.system("vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol")
Basically, im using os.system to launch a program name vmd with another script i wrote and im giving it one argument. What i found it is that when i run this test script, i get nothing done but if i just go in terminal and write :
vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol
everything works perfectly. Is there anything im doing wrong with os.system? I have been using this line for a while now but on linux and it was working perfectly, could it be a mac issue?
Thanks allot
import subprocess
ls_output = subprocess.check_output(['vmd', '-eofexit', '<', 'VMD_script.tcl', '-args', '3spi_cholesterol'])

Categories