Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions Lab2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#Lab 2
##File processing in Python
How do you ask if a file exists?

```
import os
os.path.isfile('./file')
```
How to you ask if a file is a directory?

```
import os
os.path.isdir('./dir')
```
How do you remove (delete) a file?

```
import os
os.remove('./file')
```
How do you get the size of a file?

```
import os
statinfo = os.stat('file')
statinfo.st_size
```
How do you get all the file names matching a pattern?

```
import glob
glob.glob('pattern')
```
How do you get all the file names matching a pattern recursively?

```
import glob
glob.glob(/*/'pattern')
```
How do you get an iterator to all files matching a pattern, as opposed to returning a potentially huge list?

```
import glob
glob.iglob('pattern')
```
How do you open gzip-compressed files for reading and for writing?

_reading:_

```
import gzip
with gzip.open('file.txt.gz', 'rb') as f:
file_content = f.read()
```
_writing:_

```
import gzip
with gzip.open('file.txt.gz', 'wb') as f:
file_content = f.write()
```


##In R
How do you ask if a file exists?

```
file.exists("file")
```
How to you ask if a file is a directory?

```
dir.exists("dir")
```

How do you remove (delete) a file?

```
file.remove("file")
```
How do you get the size of a file?

```
file.info("file")
```

How do you get all the file names matching a pattern?

```
list.files(pattern="pattern_you_want")
```

How do you get all the file names matching a pattern recursively?

```
list.files(pattern="pattern_you_want", recursive=TRUE)
```

How do you open gzip-compressed files for reading and for writing?

```
library(R.utils)
gunzip("file.gz")
```

##Python

```{py}
import glob
import os.path
output=[]
for file in glob.glob('./lab2data/*/*'):
base=os.path.basename(file)
rawfile=os.path.splitext(base)[0]
raw2=rawfile.split(".",1)[1]
for i in raw2:
output.append(raw2)
myset = set(output)
my_list = ['outfile.' + x + '.out' for x in myset]
print(my_list)

```

##R

```
library(tidyverse)
files <- list.files(recursive = TRUE, full.names = FALSE)
files <- files %>%
gsub(., pattern = "data/datafile.", replacement = "") %>%
gsub(., pattern = "output/outfile.", replacement = '') %>%
gsub(., pattern = ".txt", replacement = "") %>%
gsub(., pattern = ".out", replacement = "") %>%
unique_files <- unique(files)
unique_files <- paste0("outfile.", unique_files, ".out")
```