Why does file.append write "hello world" as duplicate in csv file - groovy

When I run the following code:
f = new File(projectroot + "/outputTest1.csv")
f.append("hello world" + '\n')
Here is the result in csv file:
hello world
hello world
Why is it displaying a duplicate?
Take note: I'm using groovy in soapUI

Append adds given string to the end of the file, so when you run it for the third time, it should display 3 hello worlds.

Two possible explanations there :
You're not clearing the file content before writing to it, which means you'll add a new line with "Hello world" every time you execute your code. Either :
use the "Write" method instead of "Append"
clear the file using file.bytes = new byte[0]
You call this piece of code twice, which can be checked by adding a log/display and checking if your log only appears once.
Further reading :
SO question about file clearing
Groovy documentation about file handling

I was using this code inside of an assertion of SoapUI then created duplicate. When I added a separate groovy step with the same code then it worked correctly.

Related

file.txt content disappearing after calling the next function

I am trying to get my program to write over a simple config.txt file.
Which is actually working: it writes the new content over the old one if one condition is met and proceeds to the next block.
However, when the next block calls another function of my program, the content of the file simply dissapears: the .txt file becomes empty.
I have searched SO for similar issues but couldn't find any posts.
I looked into:
How to search and replace text in a file using Python?
Search and replace a line in a file in Python
And and got some of these to work but always ran into the issue above.
Please be gentle as I am a newbie and this is my first "not Hello_world" program and my first post. Thanks:)
>>>Content of the default config.txt file is 'runcount=0'
def alias(confirmation):
###First block###
if confirmation.lower()=='yes':
print("...")
config=open('config.txt','r')
data=config.read()
config.close()
newdata=data.replace('runcount=0','runcount=1')
config=open('config.txt','w')
config.write(newdata)
config.close()
confirmation2=input()
>>>Content of the config.txt file is now 'runcount=1'
###Second block###
if confirmation2.lower()=='yes':
print("...")
return main()
else:
...
>>>The main() function is called... However the config.txt file is now empty
I choose to show you this code because I think it is the most readable.
I tried the "with" method which gives me the same results.
I also tried to write to a new file, remove the default and rename the new to config.txt... same issue.
I don't get why the file becomes empty as I closed it.
No error msg, my program just goes on but the file is empty.
Please note that I used open(config.txt, 'r') in another function of my program, only once yet.
It happens that I coded the following line in main() and did not close config.txt:
config=open("config.txt","w+")
And forgot about it...
Thank you guys for the hint.
I'm going to hide somewhere very dark now.

Attempting to append all content into file, last iteration is the only one filling text document

I'm trying to Create a file and append all the content being calculated into that file, but when I run the script the very last iteration is written inside the file and nothing else.
My code is on pastebin, it's too long, and I feel like you would have to see exactly how the iteration is happening.
Try to summarize it, Go through an array of model numbers, if the model number matches call the function that calculates that MAC_ADDRESS, when done calculating store all the content inside a the file.
I have tried two possible routes and both have failed, giving the same result. There is no error in the code (it runs) but it just doesn't store the content into the file properly there should be 97 different APs and it's storing only 1.
The difference between the first and second attempt,
1 attempt) I open/create file in the beginning of the script and close at the very end.
2 attempt) I open/create file and close per-iteration.
First Attempt:
https://pastebin.com/jCpLGMCK
#Beginning of code
File = open("All_Possibilities.txt", "a+")
#End of code
File.close()
Second Attempt:
https://pastebin.com/cVrXQaAT
#Per function
File = open("All_Possibilities.txt", "a+")
#per function
File.close()
If I'm not suppose to reference other websites, please let me know and I'll just paste the code in his post.
Rather than close(), please use with:
with open('All_Possibilities.txt', 'a') as file_out:
file_out.write('some text\n')
The documentation explains that you don't need + to append writes to a file.
You may want to add some debugging console print() statements, or use a debugger like pdb, to verify that the write() statement actually ran, and that the variable you were writing actually contained the text you thought it did.
You have several loops that could be a one-liner using readlines().
Please do this:
$ pip install flake8
$ flake8 *.py
That is, please run the flake8 lint utility against your source code,
and follow the advice that it offers you.
In particular, it would be much better to name your identifier file than to name it File.
The initial capital letter means something to humans reading your code -- it is
used when naming classes, rather than local variables. Good luck!

How to run a string from an input file as python code?

I am creating something along the likes of a text adventure game. I have a .yaml file that is my input. This file looks something like this
node_type:
action
title:
Do some stuff
info:
This does some stuff and things
script:
'print("hello world")
print(ret_val)
foo.bar(True)
ret_val = (foo.bar() == True)
if (thing):
print(thing)
print(ret_val)
'
My end goal is to have my python program run the script portion of the yaml file exactly as if it had been copy pasted into the main code. (I know there are about ten bazillion security reasons I should not be running user input like this, but I am the only one writing these nodes, and the only one using this program so I'm mostly just ignoring this fact...)
Currently my attempt goes like this: I load my yaml file as a dict using pyyaml
node = yaml.safe_load(file.yaml)
Then I'm trying to use exec to run my code and hitting a lot of problems, I can't run if statements, I simply get a syntax error, and I can't get any sort of return value from my code. I've tried this as a work around:
def main()
ret_val = "test";
thing = exec(node['script'], globals(),locals())
print(ret_val)
which when run with the above .yaml file prints
>> hello world
>> test
>> True
>> test
for some reason not actually modifying any of my main variables even though I fed them to exec.
Is there any way for me to work around these issues or is there an all together better way to be doing this?
One way of doing this would be to parse the code out and save it to a .py file, from which it can be imported dynamically, for example by importlib.
You might want to encapsulate parsed code into a function, which you can then easily call to invoke your action. Also, it would make sense to specify some default imports there.

neo4j and Groovy

I am trying to use a script from https://gist.github.com/jexp/0617412dcdd644fd520b. There is no authors.csv file, so I created my own test file that has 3 columns: author, title and date. Then I tried to run the groovy script and got the following error
Caught: groovy.lang.MissingPropertyException: author
groovy.lang.MissingPropertyException: author
at com.xlson.groovycsv.PropertyMapper.propertyMissing(PropertyMapper.groovy:52)
at test.run(test.groovy:55)
I don't know Java and I am wondering if someone can point what happened here.
[I was running the first script that uses only 1 input file]
Added explanation. My CSV file looks like this
author title date
Auth1 Title1 date1
... ... ...
You will need to add a header line to your CSV:
author,date,title
(Assuming that's the order in your CSV)
I faced the same issue with groovyCSV 7 years later...
The answer comes probably too late for you, but maybe it will help someone else.
In my case I was able to call every other column without any problem, but calling the first one always returned a MissingPropertyException exception.
Problem was that I created my csv file with Excel and this stupid software adds a BOM (byte order mark) at the beginning of the csv files, which prevented groovyCSV from reading correctly the header value of the first column.
Cleaning the csv file with a dos2unix command fixed the problem.

learnyounode 'My First I/O' example

This program puzzles me. The goal of this program is to count the number of newlines in a file and output it in command prompt. Learnyounode then runs their own check on the file and sees if their answer matches your answer.
So I start with the answer :
var fs = require('fs');
var filename = process.argv[2];
file = fs.readFileSync(filename);
contents = file.toString();
console.log(contents.split('\n').length - 1);
learnyounode verifies that this program correctly counts the number of new lines. But when I change the program to any of the following, it doesn't print out the same number as learnyounode prints out.
file = fs.readFileSync(C:/Nick/test.txt);
file = fs.readFileSync(test.txt);
Shouldn't nodejs readFileSync be able to input an address and read it correctly?
Lastly, this program is supposed to print out the # of newlines in a program. Why does both the correct program and learnyounode print out the same number that is different from the amount of newlines everytime I run this program?
For example, the number of newlines in test.txt is 3. But running this program prints out a different number everytime, like 45, 15, 2, etc. Yet at the same time, it is verified as a correct program by learnyounode because both their answers match! What is going on?
EDIT:
test.txt looks like this
ok
testing
123
So, I tried your program on my local machine and your program works fine. I am not an expert on learnyounode. I just tried it after your question but I think I understand how it works. As such, here are the answers to your questions:
Shouldn't nodejs readFileSync be able to input an address and read it correctly?
This method from nodejs is working fine. You can try printing the contents of the file and you'll see that there are no problems.
Why does both the correct program and learnyounode print out the same number that is different from the amount of newlines everytime I run this program.
learnyounode is running your program with a different filename as input each time. It verifies the output of your program by running its own copy of correct code against the same file.
But when I change the program to any of the following, it doesn't print out the same number as learnyounode prints out.
That is because at this point, your code is processing a fixed file whereas learnyounode is still processing different files on each iteration.
This tripped me up too. If you read the learnyounode instructions closely they explicitly say...
"The full path to the file to read will be provided as the first command-line argument."
This means they are providing the path to their own file.
When you use process.argv[2], this is passing in the 3rd array item (the learnyounode test txt file) into your script. If you run a console.log(process.argv); you'll see the full array object looks something like this:
[ '/usr/local/bin/node',
'/Users/user/pathstuff/learnyounode/firstio.js',
'/var/folders/41/p2jvc80j26l7nty0sk0zs1z40000gn/T/_learnyounode_1613.txt' ]
The reason the validation numbers begin to mismatch when you substitute your own text file for their is because your file always has 3 lines whereas their unit tests keep passing in different length files via process.argv.
Hope that helps.
when you are using process.argv[2] in learnyounode, the argument is provided by learnyounode automatically, so it prints different number of lines like 45, 15, 2 etc at multiple times verification.
If you remember the second challenge "BABYSTEPS" carefully this was given:
learnyounode will be supplying arguments to your program when you run
learnyounode verify program.js so you don't need to supply them yourself.
That's why different line numbers at program.js verification on multiple times.
there are two different ways.
if you run program like:
node program_name.js
than you need to add path to text file:
node program_name.js text_file.txt
in this case make sure that files are in the same directory.
or you can run it with command:
learnyounode program_name.js
and than default text file will be provided by learnyounode. You can watch content of this text file by using
console.log(buffer)
Problem statement says
The full path to the file to read will be provided as the first
command-line argument.
So you've to pass the path/to/file as an argument.
Remember process.argv
you should use the following method to execute .js files
node program_name.js /path/to/text_file_name
rather than
learnyounode run program_name.js /path/to/text_file_name
on this method, Node.js will run your program with specify files of you enter on the command-line-interface.
wish this answer can help you programming. :)

Resources