Jul-13-2020, 04:10 PM
(Jul-13-2020, 02:58 PM)12237ee1 Wrote: I am using linux so where should I put the path of the file in the script
I tried it like this
a= "/home/file.txt"
pdf_files = set(a)
but it didn't work .. also I need the result without brackets only the name of one file in each line
thank you
Usually you save your data in your homedirectory.
If your username is
12237ee1
, then path to your home directory is: /home/12237ee1/
. Put your file into your home directory. If you open your terminal window, you're already loged in with your user. Use pwd
to show the Path where you are.Applying the set on a regular
str
does not what you want:In [1]: set("/home/file.txt") Out[1]: {'.', '/', 'e', 'f', 'h', 'i', 'l', 'm', 'o', 't', 'x'}You need the lines in a sequence and then you can consume them with a set.
with open("/home/12237ee1/file.txt") as fd: unique_lines = set(fd)
fd
is an iterator, which yields lines (with line ending).The
set()
takes those elements. Identical lines are removed and the order is not preserved.But these are the basics. You need to know what a file object is, what iterables are and what the different data types does with it.
Otherwise, you'll not understand how Python works. I call it brute-force programming, what you do.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!