Python reading text file and using the in function - in-function

I am learning how to program in python for school and I apologize if my question is very basic. But I am attempting to have python read a txt file on my macbook that is called word.txt for an exercise for a book called think_python for class. Below is the code I am attempting to use to have the file read by python
import os
f = open(os.path.expanduser("~/Downloads/word.txt"))
f.readlines()
I then run the in function to try and have python see if the word "uno" is in the word.txt file.
"uno" in f
Python returns the in function as false, but I do have the word as "uno" in the text file, I have also tried removing the quotes and just uno in the txt file and same result. The assignment asks that I return true indicating that python does see the word "uno" and returns true for the in function. Can someone tell me what I am doing wrong? thank you
Also when I do a print(f) I do see that python returns the following:
print(f)
<open file '/Downloads/word.txt', mode 'r' at 0x207ed10>
Does that mean that python is reading the file correctly?

Related

Ipython Notebook writefile and execute cell at the same time

When using the magic cell %%writefile, it seems that ipython treat the content of the cell as plain txt and will not execute it.
Is there any way to run the code in the cell and then automatically export it to some script file?
This is useful because it gives you the ability to programmatically sync your notebook with a script that only keep some parts of the notebook.
In the notebook, some codes are only scaffolds, downloading the notebook as a py script will include these unnecessary codes. Meanwhile, you may want to re-structure the document, put two code blocks together while in the notebook there are a lot of draft content between them.
Ugly, all of the above things can be done by copying codes manually. But it will make future maintenance full of tedious recopy and paste work.
Thanks to author for answer. I think full code will be useful for others:
from IPython.core.magic import register_cell_magic
#register_cell_magic
def write_and_run(line, cell):
argz = line.split()
file = argz[-1]
mode = 'w'
if len(argz) == 2 and argz[0] == '-a':
mode = 'a'
with open(file, mode) as f:
f.write(cell)
get_ipython().run_cell(cell)
You can declare this in your Jupiter notebook. Mark cells like:
%%write_and_run some.py
or
%%write_and_run -a some.py
Well, seems that those using ipython don't care my question. I think I have sorted out the solution by myself.
Actually, you can write your own magic command following this post. The key command to use related to ipython is
self.shell.run_cell(cell)
You can write a magic command that firstly executes the cell using the above command and then save the cell content to some file. Ipython will pass the line and cell args as unicode object.
I have implemented one that does the following job.
run the code
write the code to some file given by -f arg
if a -before arg(a str) is given, then put the code just before that str.
if an -indent arg is given, then write a indented version of cell

fprintf after opening file using fopen does not work in matlab

May be my tired eyes are ignoring something trivial. But I am not able to get the following code work in matlab. I need to open an existing file and write text to it.
filename='E:\\data_bak\\test.txt';
fileId=fopen(filename,'r+');
if(fileId~=-1)
myline=fgetl(fileId);
nbytes=fprintf(fileId,'%s\n','testdata')
fclose(fileId);
end
I am using matlab 7.9.0 on windows 7. I have tried rt+ for permissions in fopen. Also, fileId is not equal to zero as I am able to read the line in the variable myline. In addition, fprintf('%s\n','testdata') successfully prints testdata on the matlab prompt.
The value assigned to nbytes is 9. So it does appear it has written to file but when I open the file in text editor, I am not able to find text 'testdata'.

Use a variable as a file name for pdflatex

I've used MATLAB and pdfLatex but now I'm trying to combine them. I've found that you can call pdflatex with a ! in MATLAB and that seems to work but I want the filename that get's latexed to be dynamic (i.e. a variable) Here is my code (MATLAB) functionally.
for loop starts here
filename = concatinate(these variables/text strings.tex)
open file
write (fprintf) this latex code to filename (this part works)
close file
!pdflatex filename
for loop ends here
If I type the exact file name (file.tex) after !pdflatex it works, but the filename changes and I need it to produce about 80 files. Is there a way to make pdflatex evaluate the variable so that I can create these files in the code instead of one by one?
Thanks. Gizm0
Update: Thanks to #Luis Mendo my program is now calling pdflatex and working on some of the files, but I am getting this error on many lines.
! Text line contains an invalid character.
Here is an example of one of the lines:
\noindent
When I copied and pasted the line above that is what showed up, but the line on the screen looks like this:
\noindent Good Instructor. \\
If I retype the line it works fine, but there is something weird about the way MATLAB is writing the line to the tex file that is making it invalid. The \noindent and \ are printed directly. "Good Instructor." comes from a variable.
I tried converting the tex file to a txt file and now that line looks like this:
\noindent G o o d i n s t r u c t o r . \\
Any ideas?
Use system instead of !. It does the same thing but lets you build the string dynamically:
system(['pdflatex ' filename])

Problem referencing executable using MATLAB "dos" function

I have a fairly simple question that has me stymied. I am trying to run an executable built from a simple C program using MATLAB as a shell, i.e. using the following MATLAB code:
FileName = ['D:\Users\person\Desktop\MATLAB\GUI','\Program.exe &'];
dos(FileName);
The executable correctly begins running, but crashes giving the error:
Debug Assertion Failed!
Program: D:\Users\person\Desktop\MATLAB\GUI\Program.exe
File: f:\\dd\vctools\crt_bld\self_x86\crt\src\fscanf.c
Expression: (stream != NULL)
The programs opens a text file, reads the input, performs math functions, and writes outputs back to another text file. I assume that this error means there is a problem reading from the text file, BUT-- when I run the executable by itself (i.e. Windows Explorer doubleclick), it executes flawlessly, as I would expect.
So, it's only MATLAB pointing to the file location that is causing the crash. Any ideas? Thank you.
Sounds like relative paths are the culprit. The Matlab command is running from whatever directory you've specified within Matlab; cmd runs from root or something like that (don't know much about Dos). That's why it works when you specify absolute paths. Change your Matlab directory to DOS root, and see if it works as originally coded.
Edit: Note that it's the path to whatever file Program.exe is trying to read that's the problem, not the path to Program.exe itself.

How to import a macro file (previously exported as .bas file) to Microsoft Word using command line?

I'm writing a command-line program that has a step in which I need to replace text in a Word file. The replacing task is accomplished using Word macro.
What I need to do now is to call this macro from command-line. At the moment we can do this by using the /mMacroName parameter of 'winword.exe', i.e. <path-to-msoffice>\winword.exe /mMacroName. But this needs the macro to be already available as a global macro.
Since I need to run the program on another computer, I need to import the above replacing macro programmatically... and I don't know how to do this.
Adding the macro using VBScript would be an option. You can find a sample to get started in the following related question:
Remove MS Word macro using VBScript