I have copied a file in my current working directory. This file has a text:
order value=78
I want to replace this text with new one
parcel value= 500
How can I do this?
Using Get File from OperatingSystem Library you can read the contents of the file,
and using Replace String from String Library you can replace the string and
using Create File from OperatingSystem library you can create the file.
If the file, in which you want to make replacement, is large AND you don't want to load the whole file contents in a variable, then you can make use of Run keyword from OperatingSystem Library. The keyword will execute the shell command, here it is "sed" and make the replacement.
(Of course, you should be on Linux in this case)
For example:
*** Settings ***
| Library | OperatingSystem
*** Test Cases ***
| Example of replacing the text in a file
| | ${result}= | Run | sed -i 's/foo/bar/g' myfile.txt
The variable result will hold the stdout which you get after running the shell command.
I am using a python script for this. Put the code below in a .py file and load this in your robot file using Library in the settings part.
Next in the robot file use this keyword as:
Replace line in file | file | searchExp1 | replaceLine
def Replace_line_in_file(file,searchExp1,replaceLine):
""" Open a file (like input.txt) and find the line that
contains the string searchExp1.
and replace that complete line by replaceLine.
When there are multiple lines that contain searchExp1
then all those lines will be replaced
"""
for line in fileinput.input(file, inplace=1):
if searchExp1 in line:
line = replaceLine+'\n'
sys.stdout.write(line)
I tried using the above keyword "Replace line in file".
But I get the error: Exception TypeError: TypeError("'NoneType' object is not callable",) in > ignored
What other changes are required.
Related
Could someone steer me towards a resource to do the following in unix: I want to set a variable equal to a filename so that I can input that variable/filename into a command line tool. I am trying to automate the process of running this command line tool by doing so.
My input files will always have the same string at the end their unique names.
How can I get this filename by searching the directory for a string AND successfully input that variable into command line tool?
so the unix code would look something like:
file1="find . -maxdepth 1 -name "string""
my command line tool --input $file1
thanks for your patience!
P.S only one file with that string will be in a directory at a time.
Instead of work with variables you can directly use the output of find command as parameter in your command line:
my_command_line_tool --input "$(find . -maxdepth 1 -name "*string*")"
If you expect more than one file you may remove the outer quotation marks. But this may broke the command if you have files which match the string, but have special characters like space, new line, etc in filename.
I have a folder that contains a lot of files. In this case images.
I need to organise these images into a directory structure.
I have a spreadsheet that contains the filenames and the corresponding path where the file should be copied to. I've saved this file as a text document named files.txt
+--------------+-----------------------+
| image01.jpg | path/to/destination |
+--------------+-----------------------+
| image02.jpg | path/to/destination |
+--------------+-----------------------+
I'm trying to use rsync with the --files-from flag but can't get it to work.
According to man rsync:
--include-from=FILE
This option is related to the --include option, but it specifies a FILE that contains include patterns (one per line). Blank lines in the file and lines starting with ';' or '#' are ignored. If FILE is -, the list will be read from standard input
Here's the command i'm using: rsync -a --files-from=/path/to/files.txt path/to/destinationFolder
And here's the rsync error: syntax or usage error (code 1) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/options.c(1436) [client=2.6.9]
It's still pretty unclear to me how the files.txt document should be formatted/structured and why my command is failing.
Any help is appreciated.
I need to remove a value from file in local directory.
The format of the file is .txt. I need to remove a complete line.
How can I do this using Robot Framework?
A similar question to this has been asked before: Looping through the content of a file in RobotFramework. Using the example from this link I created a file called test.csv with the following content:
1001
1002
1003
1004
Then proceeded to create a robot file with the following code. It reads the csv file into memory, deletes that file and creates a new one with the same name. The proceeds to loop through the in-memory version and writes every line into the file with the exception of 1 line holding the value 1003.
*** Settings ***
Library OperatingSystem
Library String
*** Test Cases ***
Example of looping over the lines in a file
${contents}= Get File test.csv
Remove File test.csv
Create File test.csv
#{lines}= Split to lines ${contents}
:FOR ${line} IN #{lines}
\ log ${line}
\ Run Keyword If ('${line}'<>'1003') Append To File test.csv ${line}\n
Although this example will work, the real question is whether you should do this in Robot Script. I agree with Bryan Oakley that this would be better suited for Python where you have more control over the file.
I am using the shell() command to generate pdf documents from .tex files within a function. This function sometimes gets ran multiple times with adjusted data and so will overwrite the documents. Of course, if the pdf file is open when the .tex file is ran, it generates an error saying it can't run the .tex file. So I want to know whether there are any R or Windows cmd commands which will check whether a file is open or not?
I'm not claiming this as a great solution: it is hacky but maybe it will do. You can make a copy of the file and try to overwrite your original file with it. If it fails, no harm is made. If it succeeds, you'll have modified the file's info (not the contents) but since your end goal is to overwrite it anyway I doubt it will be a huge problem. In either case, you'll be fixed about whether or not the file can be rewritten.
is.writeable <- function(f) {
tmp <- tempfile()
file.copy(f, tmp)
success <- file.copy(tmp, f)
return(success)
}
openfiles /query /v|(findstr /i /c:"C:\Users\David Candy\Documents\Super.xls"&&echo File is open||echo File isn't opened)
Output
592 David Candy 1756 EXCEL.EXE C:\Users\David Candy\Documents\Super.xls
File is open
Findstr returns 0 if found and 1+ if not found or error.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
.
--
How can I loop through the contents of a file within Robot Framework?
My file contents would be like this:
1001
1002
1003
1004
I want to read the contents one by one, assign it to a variable and then do some operations with it.
Robotframework has several built-in libraries that add a lot of functionality. Two that you can use for this task are the OperatingSystem library and the String library.
You can use the keyword Get File from the OperatingSystem library to read the file, and you can use the Split to Lines keyword from the String library to convert the file contents to a list of lines. Then it's just a matter of looping over the lines using a for loop.
For example:
*** Settings ***
| Library | OperatingSystem
| Library | String
*** Test Cases ***
| Example of looping over the lines in a file
| | ${contents}= | Get File | data.txt
| | #{lines}= | Split to lines | ${contents}
| | :FOR | ${line} | IN | #{lines}
| | | log | ${line} | WARN
This solve my issue same like yours !
${File}= Get File Path\\FileName.txt
#{list}= Split to lines ${File}
:FOR ${line} IN #{list}
\ Log ${line}
\ ${Value}= Get Variable Value ${line}
\ Log ${Value}
I am reading from 'text' file and 'Get Variable Value' is part of builtin library. Thanks!
Below is a list of different examples how to use FOR & While loops in your Robot Framework Test Cases.
http://robotframework.googlecode.com/svn/tags/robotframework-2.5.3/atest/testdata/running/for.txt
My strategy, that I've used successfully with .csv files, would be to create a Python-based keyword that will grab the nth item in a file. The way I did it involved importing the CSV Python library, so to give a more complete answer I'd have to know what file type you're trying to read from.